Argentina
Argentina

Reputation: 1141

Remove entries from JSONArray Java

I have the following json:

{
    "scenes": {
        "A-B-C": [{
                "id": "OPENINGS",
                "value": "A"
            },
            {
                "id": "VIEW",
                "value": "B"
            },
            {
                "id": "CAMERA",
                "value": "C"
            },
            {
                "id": "VIEW",
                "value": "D"    //this should be removed
            },
            {
                "id": "CAMERA",
                "value": "E" //this should be removed
            }
        ],
        "A-D-E": [{
                "id": "OPENINGS",
                "value": "A"
            },
            {
                "id": "VIEW",
                "value": "B"   //this should be removed
            },
            {
                "id": "CAMERA",
                "value": "C"  //this should be removed
            },
            {
                "id": "VIEW",
                "value": "D"
            },
            {
                "id": "CAMERA",
                "value": "E"  
            }
        ]
    }
}

My goal is to remove from each of the two arrays the entries where the value in the "value" key does not appear in the parent key. I am using this java code:

        Set<String> keys = modelJSON.getJSONObject("scenes").keySet();
        List<Integer> remove = new ArrayList<Integer>();
        for (String keyValue : keys) {
            JSONArray array = modelJSON.getJSONObject("scenes").getJSONArray(keyValue);
            remove.clear();
            int l = array.length();
            for (int i = 0; i < l; i++) {
                String target = modelJSON.getJSONObject("scenes").getJSONArray(keyValue).getJSONObject(i).getString("value");
                if (!keyValue.contains(target)) {
                    remove.add(i);
                }
            }
            Collections.sort(remove, Collections.reverseOrder());
            for (int j : remove) {
                array.remove(j);
            }
            modelJSON.getJSONObject("scenes").put(keyValue, array);
        }

In the debugger I see that the code works perfectly fine until the last line. The array variable contains exactly the entries I need before the last line is executed. The problem is, as the put function is executed the new array is replaced in each of the parent keys. Leading to this result:

{
    "scenes": {
        "A-B-C": [{
            "id": "OPENINGS",
            "value": "000002_01"
        }],
        "A-D-E": [{
            "id": "OPENINGS",
            "value": "000002_01"
        }]
    }
}

Can someone guess what is happening?

Upvotes: 1

Views: 4357

Answers (2)

Rahul ShaRma
Rahul ShaRma

Reputation: 99

Just remove that position from JSONArray

array.remove(position) notifyDataSetChanged()

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191748

array.remove(j); on its own doesn't change your JSON data. Personally, I don't see the purpose of this array variable.

Since JSON can't have duplicate keys, when you put(keyValue, array), it's overwriting the old values. Try debugging your code to watch that array shrink.

You seem to be wanting to put the remove list instead.

Make a new JSONArray, and add modelJSON.getJSONObject("scenes").getJSONArray(keyValue).getJSONObject(i) to it. Then put that

Something like this

 JSONArray newArray = new JSONArray();
JSONObject scenes = modelJSON.getJSONObject("scenes");
JSONArray array = scenes.getJSONArray(keyValue);
for (int i=0; i<array.length();i++){
    JSONObject target = array.getJSONObject(i);
    if (!keyValue.contains( target.getString("value")) ){
        newArray.put(target);
    }
}
scenes.put(keyValue, newArray);
// modelJSON.put("scenes", scenes);  // maybe necessary 

Basically, do the opposite - don't remove, but add the data you want to see and overwrite the previous data

Upvotes: 3

Related Questions