【发布时间】:2016-05-13 08:47:01
【问题描述】:
我想解析一个名为“json”的变量中的 JSON 文件格式。 但是,我收到此错误消息:
Exception in thread "main" org.json.JSONException: JSONObject["bindings"] not found.
这是我正在使用的代码:
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);
}
这是我要解析的 JSON:
{
"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" }
}
]
}
}
即使我在没有 for 循环的情况下执行以下操作,它也会一直显示相同的错误消息。
JSONObject obj = new JSONObject(json);
JSONArray bindings = obj.getJSONArray("bindings");
【问题讨论】:
标签: java arrays json jsonobject