繁体   English   中英

如何直接修改JsonObject / JsonArray的值?

[英]How to modify values of JsonObject / JsonArray directly?

一旦我将JSON字符串解析为GSON提供的JsonObject类,(假设我不希望将其解析为任何有意义的数据对象,但严格地想要使用JsonObject),我如何能够修改a的字段/值关键直接?

我没有看到可以帮助我的API。

https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonObject.html

奇怪的是,答案是继续追加财产。 我有一半期待一个setter方法。 :S

System.out.println("Before: " + obj.get("DebugLogId")); // original "02352"

obj.addProperty("DebugLogId", "YYY");

System.out.println("After: " + obj.get("DebugLogId")); // now "YYY"

这适用于使用JSONObject修改childkey值。 导入使用的是

import org.json.JSONObject;

ex json :(在提供输入时将json文件转换为字符串)

{
    "parentkey1": "name",
    "parentkey2": {
     "childkey": "test"
    },
}

JSONObject jObject  = new JSONObject(String jsoninputfileasstring);
jObject.getJSONObject("parentkey2").put("childkey","data1");
System.out.println(jObject);

输出:

{
    "parentkey1": "name",
    "parentkey2": {
     "childkey": "data1"
    },
}

从2.3版本的Gson库开始,JsonArray类有一个'set'方法。

这是一个简单的例子:

JsonArray array = new JsonArray();
array.add(new JsonPrimitive("Red"));
array.add(new JsonPrimitive("Green"));
array.add(new JsonPrimitive("Blue"));

array.remove(2);
array.set(0, new JsonPrimitive("Yelow"));

另一种方法是反序列化为java.util.Map ,然后根据需要修改Java Map 这将Java端数据处理与数据传输机制(JSON)分开,这是我更喜欢组织代码的方式:使用JSON进行数据传输,而不是替换数据结构。

它实际上都在文档中。
JSONObject和JSONArray都可以用来替换标准数据结构。
要实现setter,只需在put(String name, Object value)之前调用remove(String name) put(String name, Object value)

这是一个简单的例子:

public class BasicDB {

private JSONObject jData = new JSONObject;

public BasicDB(String username, String tagline) {
    try {
        jData.put("username", username);
        jData.put("tagline" , tagline);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public String getUsername () { 
    String ret = null;
    try {
        ret = jData.getString("username");
    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } 
    return ret;
}

public void setUsername (String username) { 
    try {
        jData.remove("username");
        jData.put("username" , username);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public String getTagline () {
    String ret = null;
    try {
        ret = jData.getString("tagline");
    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } 
    return ret;
}
public static JSONObject convertFileToJSON(String fileName, String username, List<String> list)
            throws FileNotFoundException, IOException, org.json.simple.parser.ParseException {
        JSONObject json = new JSONObject();
        String jsonStr = new String(Files.readAllBytes(Paths.get(fileName)));
        json = new JSONObject(jsonStr);
        System.out.println(json);
        JSONArray jsonArray = json.getJSONArray("users");
        JSONArray finalJsonArray = new JSONArray();
        /**
         * Get User form setNewUser method
         */
        //finalJsonArray.put(setNewUserPreference());
        boolean has = true;
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            finalJsonArray.put(jsonObject);
            String username2 = jsonObject.getString("userName");
            if (username2.equals(username)) {
                has = true;
            }
            System.out.println("user name  are :" + username2);
            JSONObject jsonObject2 = jsonObject.getJSONObject("languages");
            String eng = jsonObject2.getString("Eng");
            String fin = jsonObject2.getString("Fin");
            String ger = jsonObject2.getString("Ger");
            jsonObject2.put("Eng", "ChangeEnglishValueCheckForLongValue");
            System.out.println(" Eng : " + eng + "  Fin " + fin + "  ger : " + ger);
        }
        System.out.println("Final JSON Array \n" + json);
        jsonArray.put(setNewUserPreference());
        return json;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM