我只想知道如何在运行时更改/更新 json 文件中的值。我在如何更新 json 文件时遇到了问题。
public void parseJSON() throws JSONException, IOException
{
InputStream is = this.getResources().openRawResource(R.raw.qdb);
byte [] buffer = new byte[is.available()];
while (is.read(buffer) != -1);
String jsontext = new String(buffer);
jsonObject = new JSONObject(jsontext);
JSONObject object = jsonObject.getJSONObject("level1");
String c = object.put("value2", "third value").toString();
String attr1 = object.getString("value1");
String attr2 = object.getString("value2");
strParsedValue="Value 1 = "+attr1;
strParsedValue+="\nValue 2 = "+attr2;
strParsedValue+="\n\n\n" + c;
txtViewParsedValue.setText(strParsedValue);
}
Json文件: {“level1”:{“value1”:“一个值”,“value2”:“两个值”}}
输出:
Value 1= one value
Value 2 = third value
{"value1":"one value","value2":"third value"}
{
"level":{
"value1":"one value",
"value2":"two value"
}
}