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?
-
can you please be more specific ?Basil Battikhi– Basil Battikhi2017年11月08日 14:07:14 +00:00Commented 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?OneCricketeer– OneCricketeer2017年11月08日 14:08:02 +00:00Commented Nov 8, 2017 at 14:08
-
thats not a well parsed jsonObject to begin with. try JsonLint.com to validate it.ajc– ajc2017年11月08日 14:10:56 +00:00Commented 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")Argentina– Argentina2017年11月08日 14:13:36 +00:00Commented 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.Argentina– Argentina2017年11月08日 14:16:25 +00:00Commented Nov 8, 2017 at 14:16
2 Answers 2
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
9 Comments
new onearray 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.Just remove that position from JSONArray
array.remove(position)
notifyDataSetChanged()