无法读取JSON文件

时间:2014-02-04 07:51:16

标签: java json eclipse io gson

请查看以下代码

JsonReader reader = new JsonReader( new BufferedReader(new InputStreamReader(s3Object.getObjectContent())) );
reader.beginArray();
int gsonVal = 0;
 while (reader.hasNext() && gsonVal < 3) {
    JsonParser  _parser = new JsonParser();
    JsonElement jsonElement =  _parser.parse(reader);
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    //Do something

    System.out.println(jsonObject.get("title"));}

我正在尝试使用GSON读取一个巨大的Json文件。 JSON文件的格式如下所示

[
    {
        "hash_index": "00102x05h06l0aj0dw",
        "body": "Who's signing up for Obamacare?",
        "_type": "ArticleItem",
        "title": "Who's signing up for Obamacare? - Jan. 13, 2014",
        "source": "money.cnn.com",
        "primary_key": 0,
        "last_crawl_date": "2014-01-14",
        "url": "http://money.cnn.com/2014/01/13/news/economy/obamacare-enrollment/index.html"
    },
    {
        "hash_index": "00102x05h06l0aj0dw0iz0kn0l@0t#0",
        "body": "Who's signing up for Obamacare?",
        "_type": "ArticleItem",
        "title": "Who's signing up for Obamacare? - Jan. 13, 2014",
        "source": "money.cnn.com",
        "primary_key": 1,
        "last_crawl_date": "2014-01-14",
        "url": "http://money.cnn.com/2014/01/13/news/economy/obamacare-enrollment/index.html"
    }
]

当我运行我的代码时,我得到了后续错误。

java.io.EOFException: End of input at line 1 column 1
at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1377)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:538)
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:332)
at HashCreator.createHash(HashCreator.java:223)
at HashCreator.<init>(HashCreator.java:65)
at Main.main(Main.java:9)

第223行是 - reader.beginArray();

这是我第一次使用此GSON而这是因为我的JSON文件太大而且由于OutOfMemoryException而无法立即加载。那么,我该怎么处理这个错误呢?

0 个答案:

没有答案