GSON How to convert json object to json array?

Now I get this JSON from the API:

{"supplyPrice": { "CAD": 78, "CHF": 54600.78, "USD": 20735.52 }} 

But prices are dynamic, so I need JSON in this form

 { "supplyPrice": [ { "name": "CAD", "value": "78" }, { "name": "TRY", "value": "34961.94" }, { "name": "CHF", "value": "54600.78" }, { "name": "USD", "value": "20735.52" } ] } 

How to do it with GSON?

+5
source share
7 answers

Thanks Rohit Patil! I changed my code in my situation and it works!

 private JSONObject modifyPrices(JSONObject JSONObj) { try { JSONObject supplyPrice = JSONObj.getJSONObject("supplyPrice"); JSONArray supplyPriceArray = new JSONArray(); Iterator<?> keys = supplyPrice.keys(); while (keys.hasNext()) { String key = (String) keys.next(); String value = supplyPrice.getString(key); supplyPriceArray.put(new JSONObject("{\"name\":" + key + ",\"value\":" + value + "}")); } JSONObj.put("supplyPrice", supplyPriceArray); return JSONObj; } catch (JSONException e) { e.printStackTrace(); } return null; } 
0
source

You need to iterate all the keys of the supplyPrice object and create a new JSONArray using this key value, and then assign a new array to specify the price key

 JSONObject changeSupplyPrice(JSONObject JSONObj){ try { JSONObject supplyPrice =JSONObj.getJSONObject("supplyPrice"); JSONArray supplyPriceArray = new JSONArray(); Iterator<?> keys = supplyPrice.keys(); while( keys.hasNext() ) { String key = (String)keys.next(); Log.e("JSON Array key",key); supplyPriceArray.put(new JSONObject("{"+key+":"+supplyPrice.getString(key)+"}")); } JSONObj.put("supplyPrice", supplyPriceArray); return JSONObj; } catch (JSONException e) { e.printStackTrace(); } return null; } 

then call the function in which you want

 try { JSONObject JSONObj = new JSONObject("{'taxes': [],'name': 'Laboriosam iusto eum','requiresShipping': false, 'taxable': true, 'sku': 'QBA84J18832', 'product': 12, 'supplyPrice': { 'CAD': 78, 'CHF': 54600.78, 'USD': 20735.52 }}"); JSONObj = changeSupplyPrice(JSONObj); Log.e("Return JSON Object",JSONObj.toString()); } catch (JSONException e) { e.printStackTrace(); } 
+2
source

Hope this part of the code helps you:

  String json = "{\"supplyPrice\": {\n" + " \"CAD\": 78,\n" + " \"CHF\": 54600.78,\n" + " \"USD\": 20735.52\n" + " }}"; Gson gson = new Gson(); JsonObject jsonObject = gson.fromJson(json, JsonObject.class); JsonObject supplyPrice = jsonObject.get("supplyPrice").getAsJsonObject(); Type type = new TypeToken<HashMap<String, Double>>() { }.getType(); HashMap<String, Double> parsedJson = gson.fromJson(supplyPrice, type); JsonArray jsonArray = new JsonArray(); for(String key : parsedJson.keySet()) { JsonObject jo = new JsonObject(); jo.addProperty("name", key); jo.addProperty("value", parsedJson.get(key)); jsonArray.add(jo); } JsonObject result = new JsonObject(); result.add("supplyPrice", jsonArray.getAsJsonArray()); 
+2
source

GSON uses POJO classes to parse JSON into java objects.

Create a java class containing variables with names and data type, the same as the keys of the JSON object. I think the JSON you get is not in the correct format.

 Class SupplyPrice{ double CAD; double CHF; double TRY } Class SupplyPriceContainer{ ArrayList<SupplyPrice> supplyPrice; } 

And your JSON should be

  { "CAD": 78, "CHF": 54600.78, "USD": 20735.52 } { "supplyPrice": [{ "CAD": 78, "CHF": 0, "USD": 0 }, { "CAD": 0, "CHF": 54600.00, "USD": 0 }, { "CAD": 0, "CHF": 0, "USD": 20735.52 }] } 

Then you can use GSON `fromJson (String pJson, class pClassType) to convert to a JAVA object

  Gson gson = new Gson() ArrayList<SupplyPrice> suplyPrices = gson.fromJson(pJsonString, SupplyPrice.class); 

Now you can use arraylist to get data.

+1
source

You can directly convert it to HashMap or TreeMap , and then move the map for all keys.

This will be the easiest way to achieve what you want.

0
source

Look at this .. can help you.

  JSONObject json= json.getJSONObject("supplyPrice"); Iterator i = json.keys(); JSONArray jsonArray = new JSONArray(); while (i.hasNext()){ String key = (String) i.next(); jsonArray.put(json.get(key)); 
0
source

We are only one line of code from converting jsonObjects to arrays (not jsonArrays, but after creating the array, you can also easily create jsonArray).

 fun <LIST_TYPE>jsonObjectToArray(jsonObject : JsonObject,listTypeClass : Class<LIST_TYPE>) = arrayListOf<LIST_TYPE>().apply { jsonObject.keySet().forEach { this.add(Gson().fromJson(jsonObject.get(it).toString(),listTypeClass))}} 
0
source

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


All Articles