0

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"
}
}
4
  • Your conditional while statement does not have opening and closing curley brackets! Commented Feb 1, 2013 at 15:10
  • until where will i put the curly braces? Commented Feb 1, 2013 at 16:29
  • Should be this format: while(condition to test){ code to execute here } Commented Feb 1, 2013 at 17:37
  • Basically '{' opening bracket at while condition line and '}' closing bracket after the last or other line depending on the code you want to execute. Also no ';' colon needed. put '{' instead of';' Commented Feb 1, 2013 at 17:43

1 Answer 1

1

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

    }
Sign up to request clarification or add additional context in comments.

1 Comment

I think you need to give more clarity/information as to what exactly you trying to acheive, what the json file is used for, does this file have a path name, what the different variables are used for, perhaps find out what the various classes and functions do in your program, and anything else relevant. Do a little research and come back with what you have, present it to people then you are going to help people answer your question and people are more likely to help you if you also try to help. Also remember to give people reputation points to show your appreciation for their helping you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.