GSON. How to convert json object to json array?
Solution 1
Thanks to Rohit Patil! I modified his code to 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;
}
Solution 2
Hope this part of code to help 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());
Solution 3
GSON uses POJO classes to parse JSON into java objects.
Create A java class containing variables with names and datatype same as keys of JSON object. I think the JSON you are getting is not in the right 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's `fromJson(String pJson, Class pClassType) to convert to JAVA object
Gson gson = new Gson()
ArrayList<SupplyPrice> suplyPrices = gson.fromJson(pJsonString, SupplyPrice.class);
Now you can use the arraylist to get the data.
Solution 4
You need to iterate all keys of supplyPrice object, and create New JSONArray using that key value then assign new array to supplyPrice 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 where 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();
}
Joe Richard
Updated on July 20, 2022Comments
-
Joe Richard almost 2 years
Now I am getting this JSON from API:
{"supplyPrice": { "CAD": 78, "CHF": 54600.78, "USD": 20735.52 }}
But prices are dynamic, that's why 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 this using GSON?