0

How can I update a JSON data in Android. I get several data sets and I want to change one. How can I do this?

Trinimon
  • 13,839
  • 9
  • 44
  • 60
emrerme
  • 31
  • 1
  • 1
  • 8

2 Answers2

4

You can easily override the value you want, simply putting the same Object (key, value pair with the same key and different value) again in the JSONObject.

E.g:-

String jsonStr = "{\"a\" = 1, \"b\" = 2}";
try {
    JSONObject json = new JSONObject(jsonStr);
    json.put("a", 3);
} catch (JSONException e) {}

Modify this as per your needs and JSON structure.

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

You can take reference from

http://www.vogella.com/articles/AndroidJSON/article.html#androidjson_write

  public void writeJSON() {
      JSONObject object = new JSONObject();
      try {
        object.put("name", "Jack Hack");
        object.put("score", new Integer(200));
        object.put("current", new Double(152.32));
        object.put("nickname", "Hacker");
      } catch (JSONException e) {
        e.printStackTrace();
      }
      System.out.println(object);
    } 
Vipul
  • 27,808
  • 7
  • 60
  • 75