Simple code to convert JSONObject to list or JSONArray?

I read various topics and found similar problems, but could not find a solution to my specific problem.

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

Return:

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

I am trying to get this data into an array / list / everything where I can use keys 470,471 to retrieve data.

Any suggestions or pointers really appreciate 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 need, but I cannot do orr.get ("471"), because I do not know what the number is.

EDIT: Apparently, I cannot answer my question within 8 hours:

Thanks to the help of a friend and some vocalists, I found a solution, I am sure that this is not the most eloquent, but this is exactly what I wanted:

 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
source share
4 answers

Thanks to the help of a friend and some vocalists, I found a solution, I am sure that this is not the most eloquent, but this is exactly what I wanted:

 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.

+1
source

The data in your answer is of type JSONObject (see curly braces {}). Thus, the top-level object has two โ€œfieldsโ€, 470 and 471. Both of the data returned by these fields are arrays. These arrays have only one element, which are both objects. So here is an example of data extraction:

 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 that you have pulled it out, you can convert this data into any structure you like to make it more convenient to access from this point forward.

0
source

You can use a library that supports data binding. You can try Genson http://code.google.com/p/genson/ , it is fast, easy to use and has some nice features. Here is an example of 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
source

You can also do:

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

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

0
source

Source: https://habr.com/ru/post/1442766/


All Articles