0

I'm using org.json package for parsing and manipulating jsonobject. Attached is the structure of my json doc. I'm accessing w by the following code :

JSONObject temp = "json document";

JSONObject w_value = temp.getJSONObject("w");

s is a child object inside w. And s has child objects inside it and it goes on like that. I want only key/value pair of w. I want to get rid of s child object completely and its child objects. Any idea how this can be done? I tried org.json package. It doesn't have a method to remove a child object.

joce
  • 9,624
  • 19
  • 56
  • 74
  • http://stackoverflow.com/questions/8740377/how-to-remove-jsonarray-element-using-java - this might be helpful to you. – Prabhakaran Sep 15 '15 at 20:52
  • hi prabhakaran, thanks for the reply. I checked this link already. its to remove array having jsonobjects. I have a json parent and child inside that. – SDevInTest Sep 16 '15 at 17:03

1 Answers1

0

w_value.remove("s"); this will leave you with your 'w' object and remove the 's' object and everything inside.

--per the Json documentation: "Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key."

Not sure if you would need that last part but there you go. Hope this helps.

SteveManC
  • 132
  • 4
  • 12
  • hi steve! w_value.remove("s") accepts a string value of key which we want to remove. I've tried it already. If the key is a json object, It will throw an exception. – SDevInTest Sep 17 '15 at 12:45
  • well, the majority of my experience is with using Strings with JSON solely. You might try looking at the JsonObject.computeIfPresent method, might be able to handle what you are looking for. – SteveManC Sep 17 '15 at 15:54