0

Right now I'm using the org.json JSON library.

JSONArray data = new JSONArray();

        for (PkgLoad pkgLoad : list) {
            JSONArray row = new JSONArray();
            row.put(Double.parseDouble(df.format(pkgLoad.ounces)));
            row.put(pkgLoad.revolutions);
            data.put(row);
        }
        jsonResponse.put("aaData", data);

This is how I'm building my array. Row by row(I like doing this.) If there is a way to build the data row by row in any other libraries let me know.

Anyway. With that method I get this.

"aaData":[[2.55,6],[2.54,6],[2.53,6],[2.54,6],[2.55,6],[2.52,6],[2.55,6],[2.52,6],[2.54,6],[2.53,6]]}

That's fine. The field names are left off. With the other libraries I get the field names. So it becomes this.

 "aaData":[["ounces" : 2.55, "revolutions" : 6], etc, etc}

How do I omit this fields. Thank you very much.

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Drew H
  • 4,699
  • 9
  • 57
  • 90
  • what other libraries are you referring to? Is the data structure you want an array or a map? – mkro Jun 06 '11 at 22:53
  • Well I'm trying to ignore the property type for the JSON array. The property being ounces, revolutions. PkgLoad is just a jpa model with the fields revolutions and ounces. Gson for example has the addProperty function. It doesn't allow one to ignore the property and just write a Json row without the property. The org.json library does allow you to do this, as you can see in the row.put method. As far as the data structure it is a jpa entity object. Should I be mapping that to something else. – Drew H Jun 07 '11 at 12:49
  • It'd still be good to see what PkgLoad POJOs look like, if it has getters or fields -- most (well, GSON and Jackson at least) libs allow data binding, without explicit code. Actually, given second part, I assume it does have 2 properties "ounces" and "revolutions" – StaxMan Jun 07 '11 at 23:55

1 Answers1

2

So with Flexjson you wouldn't duplicate the work you did to build your JSON from the data model (i.e. PkgLoad). If you wanted to convert that object to JSON you'd do something like the following:

List<PkgLoad> packages = new ArrayList<PkgLoad>();
packages.add( new PkgLoad( 24.3, 6 ) );
packages.add( new PkgLoad( 45.3, 5 ) );
packages.add( new PkgLoad( 23.3, 4 ) );
...
String json = new JSONSerializer().serialize( packages, "aaData" );

That would produce JSON like the following:

{ "aaData": [ { "ounces": 24.3, "revolutions": 6 }, { "ounces": 45.3, "revolutions": 5 }, { "ounces": 23.3, "revolutions": 4 } ] }

Why? Because the list object you gave it contained an object inside it that had two fields "ounces" and "revolutions", and that's what you get back.

That's great because it's a single line to encode your JSON without requiring you write a lot of boilerplate code to translate your object model into JSON. The thing you have to be flexible on is that your JSON output will mirror (more or less) your object model design. If you're happy with that Flexjson, gson, and jackson then they will take care of all the mundane tasks of translating your object into JSON for you so you can get back to writing your code.

However, if you have to have a drastically different format of JSON from your object model your best using org.json library and doing it by hand. Trying to tell these libraries how to convert your objects into something like what you posted is going to be as much work to writing it manually.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138