user2032847
user2032847

Reputation: 1

Updating json file JSONObject in android java

I just want to know how to change/update the value in the json file during runtime. I had problems on how to update the json file.

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 file: { "level1": { "value1" : "one value", "value2" : "two value" } }

Output:

Value 1= one value
Value 2 = third value

{"value1":"one value","value2":"third value"}

{
 "level":{
  "value1":"one value",
  "value2":"two value"
}
}

Upvotes: 0

Views: 2080

Answers (1)

Mr_leighman
Mr_leighman

Reputation: 313

Should be this format: while(condition to test){ code to execute here } Could be other syntax errors as well so I am not saying that your program will work now

Example:

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;
        }

    }

Upvotes: 1

Related Questions