简体   繁体   English

如何使用Java访问JSONObject中的JSONArray?

[英]How to access JSONArray in a JSONObject using Java?

I am having a problem in accessing the JSONArray ("pages") while reading a json file in Java using the json-simple-1.1.1.jar 我在使用json-simple-1.1.1.jar读取Java中的json文件时访问JSONArray(“页面”)时遇到问题

My json file is around 32MB of size and the format is as below: 我的json文件大小约为32MB,格式如下:

{
    "id": "0912898213",
    "pages": [
        {
            "pageUrl": "http://www.example0.com",
            "results": [
                {
                    "number": "1"
                },
                {
                    "number": "2"
                }
            ]
        },
        {
            "pageUrl": "http://www.example1.com",
            "results": [
                {
                    "number": "3"
                },
                {
                    "number": "4"
                }
            ]
        }
    ]
}

Whereas, the Java code to access this json file is as below: 而访问此json文件的Java代码如下:

JSONParser parser=new JSONParser();
JSONObject pagesObject = (JSONObject) parser.parse(new     FileReader(PATH_JSON_DataExtractor));        
JSONArray jsonArray= (JSONArray) pagesObject.get("pages");

for(int i=0; i<jsonArray.size(); i++){}

PROBLEM: The jsonArray is null all the time. 问题: jsonArray始终为null。 Although, the json format is correct and it should work as expected! 虽然,json格式是正确的,并且应该可以正常工作! The above Java code works with the given sample json (also above) but the Java code doesn't work with the 32MB json file. 上面的Java代码适用于给定的示例json(同样在上面),但是Java代码不适用于32MB json文件。 The location of the json file is also correct and the format is also correct, but still I am getting this access issue! json文件的位置也正确,格式也正确,但是仍然出现此访问问题!

Where I am going wrong to access the json file? 我在哪里出错访问json文件? I have been looking around on similar questions, and I have followed the exact instructions to access the json file. 我一直在寻找类似的问题,并且已经按照确切的说明访问json文件。 But I am just lost in getting it right, therefore, looking for suggestions to make this code work. 但是,我只是迷失了正确性,因此,在寻找使该代码正常工作的建议。 Thank you very much for your time! 非常感谢您的宝贵时间!

With the below code it is working perfect for me. 使用下面的代码,它对我来说是完美的。 Can you check whether the specified file location is correct? 您可以检查指定的文件位置是否正确? Also try reading id like pagesObject.get("id") 也尝试读取id,例如pagesObject.get("id")

package json.simple;

import java.io.FileReader;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class ReadJSON {
    public static void main(String[] args) throws Exception {
        JSONParser parser = new JSONParser();
        JSONObject pagesObject = (JSONObject) parser.parse(new FileReader("/home/user/tmp/test.json"));
        System.out.println(pagesObject.get("id"));
        System.out.println(pagesObject.get("pages").getClass().getName());
        JSONArray jsonArray= (JSONArray) pagesObject.get("pages");

        for(int i=0; i<jsonArray.size(); i++){
            System.out.println(jsonArray.get(i));
        }
    }
}

And here is the content of my test.json. 这是我的test.json的内容。 Exactly same as yours 与您完全一样

{
    "id": "0912898213",
    "pages": [
        {
            "pageUrl": "http://www.example0.com",
            "results": [
                {
                    "number": "1"
                },
                {
                    "number": "2"
                }
            ]
        },
        {
            "pageUrl": "http://www.example1.com",
            "results": [
                {
                    "number": "3"
                },
                {
                    "number": "4"
                }
            ]
        }
    ]
}

And here is my dependency in maven 这是我对Maven的依赖

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

Finally its working now, but I still do not know the actual cause of the problem. 现在终于可以正常工作了,但我仍然不知道问题的真正原因。 I partitioned the file into two separate json files, and executed the code on both of them and it worked. 我将文件划分为两个单独的json文件,并在两个文件上执行了代码,然后工作了。 Now I just have to merge them and its done! 现在,我只需要合并它们并完成它! Not a good solution but couldn't find another way! 这不是一个好的解决方案,但是找不到其他方法!

用这个:-

JSONArray jsonArray= (JSONArray) pagesObject.getJSONArray("pages");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM