Java 格森。如何将json对象转换为json数组?

Java 格森。如何将json对象转换为json数组?,java,android,json,parsing,gson,Java,Android,Json,Parsing,Gson,现在,我从API获得了这个JSON: {"supplyPrice": { "CAD": 78, "CHF": 54600.78, "USD": 20735.52 }} 但是价格是动态的,这就是为什么我需要这种形式的JSON { "supplyPrice": [ { "name": "CAD", "value": "78" }, { "name": "TRY",

现在,我从API获得了这个JSON:

{"supplyPrice": {
        "CAD": 78,
        "CHF": 54600.78,
        "USD": 20735.52
      }}
但是价格是动态的,这就是为什么我需要这种形式的JSON

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

如何使用GSON执行此操作?

您可以直接将其转换为
哈希映射或树映射,然后在地图中导航所有键


这是实现所需的最简单的方法。

您需要迭代supplyPrice对象的所有键,并使用该键值创建新的JSONArray,然后将新数组分配给supplyPrice键

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;
}

希望这部分代码能够帮助您:

    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());
String json=“{\”supplyPrice\”:{\n+
“CAD\”:78\n+
“\“CHF\”:54600.78\n”+
“\“USD\”:20735.52\n”+
"      }}";
Gson Gson=新的Gson();
JsonObject JsonObject=gson.fromJson(json,JsonObject.class);
JsonObject supplyPrice=JsonObject.get(“supplyPrice”).getAsJsonObject();
Type Type=new-TypeToken(){
}.getType();
HashMap parsedJson=gson.fromJson(supplyPrice,type);
JsonArray JsonArray=新的JsonArray();
for(字符串键:parsedJson.keySet()){
JsonObject jo=新的JsonObject();
jo.addProperty(“名称”,键);
jo.addProperty(“value”,parsedJson.get(key));
jsonArray.add(jo);
}
JsonObject结果=新建JsonObject();
add(“supplyPrice”,jsonArray.getAsJsonArray());

GSON使用POJO类将JSON解析为java对象

创建一个java类,其中包含名称和数据类型与JSON对象键相同的变量。我认为您得到的JSON格式不正确

Class SupplyPrice{
 double CAD;
 double CHF;
 double TRY
}

Class SupplyPriceContainer{
 ArrayList<SupplyPrice> supplyPrice;
}
然后可以使用GSON的`fromJson(字符串pJson,类pClassType)转换为JAVA对象

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

现在,您可以使用arraylist获取数据。

看看这个。。我可以帮你

 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));

多亏了罗希特·帕蒂尔!我根据我的情况修改了他的代码,结果成功了

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;
}
私有JSONObject修改价格(JSONObject JSONObject){
试一试{
JSONObject supplyPrice=JSONObject.getJSONObject(“supplyPrice”);
JSONArray supplyPriceArray=新的JSONArray();
迭代器键=supplyPrice.keys();
while(keys.hasNext()){
字符串键=(字符串)键。下一步();
字符串值=supplyPrice.getString(键);
put(新的JSONObject(“{\'name\”:“+key+”,\'value\”:“+value+”}”);
}
JSONObj.put(“supplyPrice”,supplyPriceArray);
返回JSONObj;
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}

我们只需要一行代码就可以将JSONObject转换为数组(不是jsonArray,但在将其制作成arrayList之后,您也可以轻松地创建jsonArray)

funJSONObject-ToArray(jsonObject:jsonObject,listTypeClass:Class)=arrayListOf().apply{jsonObject.keySet().forEach{this.add(Gson().fromJson(jsonObject.get(it.toString(),listTypeClass))}

如果我正确理解您的问题,您可能会收到JsonObject或JsonArray。您可以调用getAsJsonObject()方法,如果失败,则调用getAsJsonArray()。或者使用isJsonObject()方法,并根据结果调用getAsJsonObject()或getAsJsonArray()。
 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));
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;
}
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))}}