3

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");  

1 Answer 1

4

You need to pass by the results JSONObject first in order to get to your JSONArray:

JSONObject obj =  new JSONObject(json);           
JSONObject results = obj.getJSONObject("results"); 
JSONArray bindings = results.getJSONArray("bindings");     
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.