Kavin-K
Kavin-K

Reputation: 2117

How to edit the value of a json object and write it to a file in android

Format of json data inside file is like,

{
  "data": {
  "id": 0,
  "amount": 0,
  "no": 0
  }
}

I need to edit id of this file.

I used following snippet to do so,

String configFileString = getStringFromFile(configFile.toString());
JSONObject json = new JSONObject(configFileString);
json.put("id", 2);

JSONObject gg = new JSONObject();
gg.put("id",json);

writeJsonFile(configFile, gg.toString());

But the output is like,

{
  "data": {
    "data": {
    "id": 0,
    "amount": 0,
    "no": 0
    },
    "id": 2
  }  
}

What modifications should I do in my code? Please help me with this

Upvotes: 0

Views: 60

Answers (1)

ampfarisaho
ampfarisaho

Reputation: 121

String configFileString = getStringFromFile(configFile.toString());
JSONObject json = new JSONObject(configFileString);
json.getJSONObject("data").put("id", 2);

Upvotes: 1

Related Questions