0

I have this json object and i need to know the "package" of a given "name", for example, i need to know the package, of "item2", in java code

{
  "package1": {
    "name": "item1",
    "type": "i"
  },
  "package2": {
    "name": "item2",
    "type": "i"
  },
  "package3": {
    "name": "item3",
    "type": "i"
  }
}

i know how to get a specific "package" from the json with the following code:

JSONObject json = new JSONObject(jsonString);
// Get childObject json
JSONObject childObject=json.getJSONObject("package2");

but i don't know how to get the package string giving a sub item like "name".

Thanks in advance!

Lea2501
  • 311
  • 6
  • 22
  • Seems you like be looking for [JsonPath](https://github.com/json-path/JsonPath#jayway-jsonpath). – Andreas Sep 02 '20 at 03:14
  • Assuming you're using [`net.sf.json.JSONObject`](http://json-lib.sourceforge.net/apidocs/net/sf/json/JSONObject.html), if you **read the documentation**, i.e. look through the list of available methods, you'll find multiple ways of iterating the named properties of a JSON object, e.g. [`names()`](http://json-lib.sourceforge.net/apidocs/net/sf/json/JSONObject.html#names()). Now that you can iterate all the packages, the rest of the code to find the desired package should be straightforward. – Andreas Sep 02 '20 at 03:17

0 Answers0