Lê Quốc Tiến
Lê Quốc Tiến

Reputation: 27

Open and edit value in JsonObject

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 !

Upvotes: 0

Views: 3212

Answers (1)

David C Adams
David C Adams

Reputation: 1973

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.

Upvotes: 1

Related Questions