0

我只想知道如何在运行时更改/更新 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"
}
}
4

1 回答 1

1

应该是这种格式:while(condition to test){ code to execute here } 也可能是其他语法错误,所以我并不是说你的程序现在可以工作

例子:

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



               example code to execute here;
        }

    }
于 2013-02-01T17:48:28.220 回答