I have a JSON file format in a variable called "json" that I want to parse. However, I'm getting this error message:
Exception in thread "main" org.json.JSONException: JSONObject["bindings"] not found.
This is the code I'm using:
JSONObject obj = new JSONObject(json);
JSONArray bindings = obj.getJSONArray("bindings");
for (int i=0; i<obj.length(); i++)
{
JSONObject x = bindings.getJSONObject(i);
x.getJSONObject("type1").getString("type");
System.out.println(x);
}
This is the JSON I'm trying to parse:
{
"head": {
"vars": [ "type1" , "pred" , "type2" ]
} ,
"results": {
"bindings": [
{
"type1": { "type": "Collection" } ,
"type2": { "type": "has" } ,
"type3": { "type": "contributor" }
} ,
{
"type1": { "type": "Collection2" } ,
"type2": { "type": "has2" } ,
"type3": { "type": "contributor2" }
}
]
}
}
Even when I execute the following without the for loop it keeps showing the same error msg.
JSONObject obj = new JSONObject(json);
JSONArray bindings = obj.getJSONArray("bindings");