1

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?

asked Nov 8, 2017 at 14:03
5
  • can you please be more specific ? Commented Nov 8, 2017 at 14:07
  • Might be unrelated, but how did the json get created? Why are those values in the list, if they have no relation to the parent value? In other words, are you trying to clean up the json after a point where this data could be prevented? Commented Nov 8, 2017 at 14:08
  • thats not a well parsed jsonObject to begin with. try JsonLint.com to validate it. Commented Nov 8, 2017 at 14:10
  • Yes because it is part of a bigger json. Infact in the code I get it from another JSONObject called modelJSON: modelJSON.getJSONObject("scenes") Commented Nov 8, 2017 at 14:13
  • It's just that the code i use to remove elements from the json is quite straightfoward and I can't figure out why its behaviour is not what I expected. Maybe I am missing something very easy. Commented Nov 8, 2017 at 14:16

2 Answers 2

3

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

answered Nov 8, 2017 at 14:17
Sign up to request clarification or add additional context in comments.

9 Comments

remove list is the list of indices that should be removed, I remove them from the "array" variable, then i "put" the array variable in the actual json. Since, as you say, the "put" function overwrites the entry in the json. What I don't understand is why the update is done in both the parent keys simultaneously.
Maybe because you're looping over the keys, and only using one remove List object. Therefore keeping the reference between keys, and clearing the list each time
Try explicitly making it a new List. There's a difference in keeping the old object reference and making a new one
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, the new array is replaced in each of the parent keys.
I can try that but what I mean is that I think the problem is not in how the new array is built, but more probably in the following step
|
1

Just remove that position from JSONArray

array.remove(position) notifyDataSetChanged()

answered Jun 26, 2019 at 7:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.