Reputation: 2716
How do I access the array products from the below JSON string? I would ideally do this, and it works -
JSONParser parser = new JSONParser();
Object response = parser.parse(json);
JSONObject jsonObject = (JSONObject) response;
JSONObject info = (JSONObject) jsonObject.get("info");
JSONArray data = (JSONArray) jsonObject.get("products");
But the problem is the root element info is not always the same, so I can't hardcode my code. Basically, I don't want to access products by accessing info first. I want to directly access the products array.
How can I do this?
{
"info": {
"products": [
{
"test": 11577,
"number": 2541,
"product": "deoderant",
"id": 1,
"subId": 5,
"tempId": 3,
"name": null,
"lastModified": "2015-05-24",
"lastUsed": 5,
"score": 0.93,
"season": "Seasonal",
"availability": 0,
"itemRanking": null,
"itemQuantity": 5,
"listOfStores": [
7896
]
}
],
"storeId": 10145678
}
}
Upvotes: 1
Views: 86
Reputation: 1430
Use fasterxml lib,you can write code like below:
ObjectMapper mapper = new ObjectMapper();
// json is your json string
String json = "";
JsonNode jsonNode = node.findParent("products").get("products");
System.out.println(jsonNode);
Upvotes: 0
Reputation: 26077
convert to map, LinkedHashMap
or HashMap
as per your need, then get the entry value.
public class Test {
public static void main(String[] args) {
String json = "{\"info\": {\"products\": [{\"test\": 11577,\"number\": 2541,\"product\": \"deoderant\",\"id\": 1,\"subId\": 5,\"tempId\": 3,\"name\": null,\"lastModified\": \"2015-05-24\",\"lastUsed\": 5,\"score\": 0.93,\"season\": \"Seasonal\",\"availability\": 0,\"itemRanking\": null,\"itemQuantity\": 5,\"listOfStores\": [7896]}],\"storeId\": 10145678}}";
JSONObject jsonObject;
try {
jsonObject = new JSONObject(json);
Map<String, Object> map = getMap(jsonObject);
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static Map getMap(JSONObject object) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
Object jsonObject = null;
String key = null;
Object value = null;
try {
Iterator<String> keys = object.keys();
while (keys.hasNext()) {
key = null;
value = null;
key = keys.next();
if (null != key && !object.isNull(key)) {
value = object.get(key);
}
if (value instanceof JSONObject) {
map.put(key, getMap((JSONObject) value));
continue;
}
if (value instanceof JSONArray) {
JSONArray array = ((JSONArray) value);
List list = new ArrayList();
for (int i = 0 ; i < array.length() ; i++) {
jsonObject = array.get(i);
if (jsonObject instanceof JSONObject) {
list.add(getMap((JSONObject) jsonObject));
} else {
list.add(jsonObject);
}
}
map.put(key, list);
continue;
}
map.put(key, value);
}
} catch (Exception e) {
System.out.println(e);
}
return map;
}
}
Output
info
{products=[{test=11577, number=2541, product=deoderant, id=1, subId=5, tempId=3, name=null, lastModified=2015-05-24, lastUsed=5, score=0.93, season=Seasonal, availability=0, itemRanking=null, itemQuantity=5, listOfStores=[7896]}], storeId=10145678}
Upvotes: 1