This question already has answers here:
How to parse JSON in Java

(33个答案)


3年前关闭。




我有一个Json数组,在每个对象上我都想相应地编辑特定键的值

例如,我如何遍历此数组并编辑每个对象的“ unix_time”键。

[{"_id":"","__v":0,"read":false,"timestamp":"","unix_time":"1477924542020"},
{"_id":"","__v":0,"read":false,"timestamp":"","unix_time":"1477924542020"},
{"_id":"","__v":0,"read":false,"timestamp":"","unix_time":"1477924532440"},
...
...
... ]

最佳答案

下面是测试课程。请让我知道这可不可以帮你

package com.test;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONTest {
public static void main(String[] args){
JSONArray jsonArray = new JSONArray();
JSONObject j1 = new JSONObject();
j1.put("id", "1");
j1.put("read", "false");
j1.put("time", "143250176");
jsonArray.put(j1);

JSONObject j2 = new JSONObject();
j2.put("id", "2");
j2.put("read", "false");
j2.put("time", "143250177");
jsonArray.put(j2);

JSONObject j3 = new JSONObject();
j3.put("id", "3");
j3.put("read", "false");
j3.put("time", "143250178");
jsonArray.put(j3);

JSONArray newArray = new JSONArray();
for(int count=0; count<jsonArray.length(); count++)
{
    JSONObject localInstance = jsonArray.getJSONObject(count);
    if(localInstance.optString("time") != "")
    {
        String time = localInstance.getString("time");
        String newTime = "new "+time;
        localInstance.put("time", newTime);
        newArray.put(localInstance);
    }
}
System.out.println(newArray);
}
}

10-07 19:08
查看更多