Gson json 解析不工作

Gson json parsing is not working

我正在解析我通过

访问的 json 格式的文件

http://api.crossref.org/works?rows=1000&offset=6000000

大约有 7100 万条不同的发布记录,而我给出的 link 只显示了 1000 条。在我的 java 代码中,gson 解析器大部分运行良好,但有时会出现如下错误:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated string at line 2 column 1
        at com.google.gson.internal.Streams.parse(Streams.java:56)
        at com.google.gson.JsonParser.parse(JsonParser.java:84)
        at com.google.gson.JsonParser.parse(JsonParser.java:59)
        at com.google.gson.JsonParser.parse(JsonParser.java:45)
        at connectingurl.CrossRefFullTextToTable.<init>(CrossRefFullTextToTable.java:84)
        at connectingurl.CrossRefFullTextToTable.main(CrossRefFullTextToTable.java:181)
    Caused by: com.google.gson.stream.MalformedJsonException: Unterminated string at line 2 column 1
        at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1310)
        at com.google.gson.stream.JsonReader.nextString(JsonReader.java:1046)
        at com.google.gson.stream.JsonReader.nextValue(JsonReader.java:784)
        at com.google.gson.stream.JsonReader.objectValue(JsonReader.java:766)
        at com.google.gson.stream.JsonReader.peek(JsonReader.java:380)
        at com.google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:635)
        at com.google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:658)
        at com.google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:650)
        at com.google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:658)
        at com.google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:658)
        at com.google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:633)
        at com.google.gson.internal.Streams.parse(Streams.java:44)

我搜索了一下,很有可能是内存有问题。

这是我的 java 代码的一部分:

if(code == 200){

    String full_text_link = "";
    String license = "";

    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
    StringBuilder sb = new StringBuilder();

    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        sb.append(inputLine + System.getProperty("line.separator"));
    }
    in.close();
    String jsonLine = sb.toString();


    JsonElement jelement = new JsonParser().parse(jsonLine);     
    JsonObject  jobject = jelement.getAsJsonObject();
    jobject = jobject.getAsJsonObject("message");
    JsonArray jarray_items = jobject.getAsJsonArray("items");

    ...
}

错误来自以下行:

JsonElement jelement = new JsonParser().parse(jsonLine);

gson好像有这样的问题。有没有其他建议使用另一个解析器或者我应该添加我的代码?

提前致谢...

while ((inputLine = in.readLine()) != null) {
   sb.append(inputLine + System.getProperty("line.separator"));
}

你基本上是在无缘无故地修改你的输入,你不能简单地预处理你的 JSON 而不知道确切的语法,要解决这个问题你可以让 GSON 读取文件本身,例如:

http://www.java2blog.com/2013/11/gson-example-read-and-write-json.html

package org.arpit.java2blog;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.arpit.java2blog.pojo.Country;
import com.google.gson.Gson;

/*
 * @Author : Arpit Mandliya
 */
public class GSONReadingFromFileExample {

 public static void main(String[] args) {

  Gson gson = new Gson();

  try {

   System.out.println("Reading JSON from a file");
   System.out.println("----------------------------");

   BufferedReader br = new BufferedReader(
     new FileReader("E:\file.json"));

    //convert the json string back to object
   Country countryObj = gson.fromJson(br, Country.class);

   System.out.println("Name Of Country: "+countryObj.getName());


   System.out.println("Population: "+countryObj.getPopulation());

   System.out.println("States are :");

   List<String> listOfStates = countryObj.getListOfStates();
   for (int i = 0; i < listOfStates.size(); i++) {
    System.out.println(listOfStates.get(i));
   }

  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}