【问题标题】:JSON Parse (JSON Object Not Found) JavaJSON 解析(未找到 JSON 对象)Java
【发布时间】: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


    【解决方案1】:

    你需要先通过结果JSONObject才能到达你的JSONArray

    JSONObject obj =  new JSONObject(json);           
    JSONObject results = obj.getJSONObject("results"); 
    JSONArray bindings = results.getJSONArray("bindings");     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 2016-10-17
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多