Simple code for converting JSONObject to a List or JSONArray?

24.6k Views Asked by At

I've read through various threads and found similar problems, but have been pretty unsuccessful at finding a solution for my particular problem.

JSONObject orr = (JSONObject)orderRows.get("orderRows");
System.out.println("data in orr = " + orr + "orr's type = " + orr.getClass());

Returns:

data in orr = {"470":[{"locationId":2,"quantity":1,"productId":1007}],"471":[{"locationId":2,"quantity":1,"productId":1008}]}orr's type = class org.json.simple.JSONObject

I'm trying to get this data into an array/list/anything where I can use the keys, 470,471 to retrieve the data.

Any suggestions or pointers much appreciated many thanks...

To clarify:

JSONObject orr = (JSONObject)orderRows.get("orderRows");
        JSONArray orderOne = (JSONArray)orr.get("471");
        System.out.println(orderOne);
        System.out.println(orderOne.get(0));
        JSONObject orderOneKey = (JSONObject)orderOne.get(0);
        System.out.println(orderOneKey.get("productId"));

This is what I'm after, but obviously I can't do orr.get("471") as I don't know what this number will be.

EDIT: Apparently I can't answer my own question for 8 hours:

Thanks to help from a friend and some fiddling, I found a solution, I'm sure it's not the most eloquent, but it's exactly what I was after:

for(Object key: orr.keySet()) {

                JSONArray orderOne = (JSONArray)orr.get(key);
                JSONObject ordervalue = (JSONObject)orderOne.get(0);
                System.out.println(ordervalue.get("productId"));
        }

Thanks for the help and suggestions guys.

4

There are 4 best solutions below

0
On BEST ANSWER

Thanks to help from a friend and some fiddling, I found a solution, I'm sure it's not the most eloquent, but it's exactly what I was after:

for(Object key: orr.keySet()) {

                JSONArray orderOne = (JSONArray)orr.get(key);
                JSONObject ordervalue = (JSONObject)orderOne.get(0);
                System.out.println(ordervalue.get("productId"));
        }

Thanks for the help and suggestions guys.

2
On

The data in your response is of type JSONObject (see the curly braces {}). So the top level object has two "fields", 470, and 471. Both of the data returned by these fields are arrays. Those arrays only have one item each, which are both objects. So here is an example of fetching the data:

JSONObject jsonObject = (JSONObject)orderRows.get("orderRows");
JSONArray firstArray = jsonObject.getJSONArray("470");
JSONArray secondArray = jsonObject.getJSONArray("471");
JSONObject firstObject = firstArray.get(0);
int locationId = firstObject.getId("locationId");
/*...etc...*/

Now once you've pulled it out you can transform this data into any structure you like to make it more friendly to access from this point forward.

1
On

You could use a library providing databinding support. You can try Genson http://code.google.com/p/genson/, it is fast, easy to use and has a couple of nice features. Here is an example for your problem:

// first define a class matching your json
class Product {
  private int locationId;
  private int quantity;
  private int productid;

  // setter & getters
}

// then use genson
Map<String, Product[]> productsMap = new Genson().deserialize(jsonStream, new GenericType<Map<String, Product[]>>() {});
0
On

You can also do:

JSONArray jsonArr = new JSONArray();
jsonArr.put(jsonObj);

in the case where you want to put whole JSON object in JSON array.