GSON. How to convert json object to json array?

前端 未结 7 1870
没有蜡笔的小新
没有蜡笔的小新 2021-02-08 15:20

Now I am getting this JSON from API:

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

But

相关标签:
7条回答
  • 2021-02-08 16:17

    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;
    }
    
    0 讨论(0)
提交回复
热议问题