0

I have local Json file in asset folder.

I use this code to open file

try {
        is = getAssets().open("data.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();

        jsonString = is.toString();
        jsonString = new String(buffer,"UTF-8");

        myJson = new JSONObject(jsonString);
        jsonArrayData = myJson.getJSONArray("diTich");
        int leng = jsonArrayData.length();
        for(int i = 0 ; i < leng ; i++) {
            mTitle = jsonArrayData.getJSONObject(i).getString("title");
            mDescription = jsonArrayData.getJSONObject(i).getString("description");
            mAddress = jsonArrayData.getJSONObject(i).getString("address");
            mStatus = jsonArrayData.getJSONObject(i).getString("status");
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

My's Json file

{ "ABCD": [ { "title": "abcd", "description": "abc", "address": "bnc", "image": "abcg", "status": false } ] }

I retrieved value in JsonObject. Now I wanna edit value in this For example, change value of key "status" from false to true. How can I do this ? I don't know write replace it !

Thanks you guys !

1 Answers1

1

You use the JSONObject.put() methods of the JSONObject class. So, in your example you could do this:

jsonArrayData.getJSONObject(i).put("status", true);

That will clobber the value that is currently there.

David C Adams
  • 1,953
  • 12
  • 12
  • Hi DavidCAdams ! I tried you way but the value don't update ! Do you know another way ? – Lê Quốc Tiến Mar 02 '14 at 04:40
  • It may be because the JSONObject is in a JSONArray. Try this: JSONObject newObject jsonArrayData.getJSONObject(i); newObject.put("status", true); If you need the JSONArray changed, then use the JSONArray.put() method to put that new JSONObject into the JSONArray at the index (i). – David C Adams Mar 02 '14 at 04:59