一旦我将 JSON 字符串解析为 GSON 提供的 JsonObject 类(假设我不希望将其解析为任何有意义的数据对象,但严格希望使用 JsonObject),我如何能够修改一个字段/值直接用钥匙?
我没有看到可以帮助我的 API。
https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonObject.html
一旦我将 JSON 字符串解析为 GSON 提供的 JsonObject 类(假设我不希望将其解析为任何有意义的数据对象,但严格希望使用 JsonObject),我如何能够修改一个字段/值直接用钥匙?
我没有看到可以帮助我的 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
. 使用的进口是
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"));
另一种方法是反序列化为 a java.util.Map
,然后Map
根据需要修改 Java。这将 Java 端数据处理与数据传输机制 (JSON) 分开,这是我更喜欢组织代码的方式:使用 JSON 进行数据传输,而不是作为替代数据结构。
它实际上都在文档中。
JSONObject 和 JSONArray 都可以用来替代标准的数据结构。
要实现 setter,只需在 aremove(String name)
之前调用 a 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;
}