Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JSON simple解析嵌套JSON数组_Java_Json_Json Simple - Fatal编程技术网

Java 使用JSON simple解析嵌套JSON数组

Java 使用JSON simple解析嵌套JSON数组,java,json,json-simple,Java,Json,Json Simple,尝试使用json simple解析rest服务中的数据。答复如下: { "locations": [ "city" : "San Jose", "state" : "Ca", "job" : { "site" : "Main Processing", "region" : "USA" } ] } JSONParser-JSONParser=new-JSONParser(); JSONObject JSONObject=(

尝试使用json simple解析rest服务中的数据。答复如下:

{
   "locations": [
      "city" : "San Jose",
      "state" : "Ca",
    "job" : {
      "site" : "Main Processing",
      "region" : "USA"
    }
  ]
}
JSONParser-JSONParser=new-JSONParser();
JSONObject JSONObject=(JSONObject)jsonParser.parse(reader);
JSONArray数组=(JSONArray)jsonObject.get(“位置”);
对于(int i=0;i
我的问题是,从JSONArray对象获取对job元素的引用时遇到问题。“locations”引用是基本的解析,但是“job”引用在数组中定义时给我带来了问题

此外,getJSONObject似乎不是JSONArray的有效方法


这可以通过json简单库来实现吗?

方法由
org.json.JSONArray
类提供。(不使用json simple)。我在json简单文档中找不到它。因此,使用
org.json.*
包导入,您可以执行以下操作:

JSONObject jsonObject = new JSONObject(jsonAsString);
JSONArray array = jsonObject.getJSONArray("locations");

//You should check that array.length() >= 3
JSONObject job = array.getJSONObject(2);
String site = job.getString("site");

所示的示例数据完全无效。JSON规范不允许不带引号的字符串键,不允许将键值对作为数组的成员,也不允许在键值对之间省略逗号。输入错误,添加了正确的JSON响应。它仍然无效,因为
位置
数组中存在键值对。这无法获取“站点”标记数据。得到一个错误,基本上说元素不存在。有专门针对嵌套对象的json库吗?有。但我的密码错了。我更新了我的答案。
JSONObject jsonObject = new JSONObject(jsonAsString);
JSONArray array = jsonObject.getJSONArray("locations");

//You should check that array.length() >= 3
JSONObject job = array.getJSONObject(2);
String site = job.getString("site");