我正在解析json格式的文件,我通过
访问http://api.crossref.org/works?rows=1000&offset=6000000
大约有7100万个不同的发布记录,我给出的链接只有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$25.read(TypeAdapters.java:635)
at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:658)
at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:650)
at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:658)
at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:658)
at com.google.gson.internal.bind.TypeAdapters$25.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可能有这样的问题。有没有其他建议使用另一个解析器或有什么我应该添加我的代码?
提前致谢...
答案 0 :(得分:1)
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();
}
}
}