2
(Google의 GSON 라이브러리를 사용하여) 일부 JSON 구문 분석에 어려움을 겪고 있습니다. 나는 다음과 같은 예외가개체의 끝에 JSON 스트림 구문 분석이 충돌합니다
Gson gson = new Gson();
String line = "{\"method\":\"GET\",\"status\":\"200 - OK\"}";
AnalysisReport report = gson.fromJson(line, AnalysisReport.class); //this works
JsonReader reader = new JsonReader(new StringReader(line));
reader.setLenient(true); //accept malformed input
while (reader.hasNext()) {
report = gson.fromJson(reader, AnalysisReport.class); //this doesn't work
}
reader.close();
: 여기
내 최소한의 코드 예제입니다 클래스는 다음과 같습니다Expected BEGIN_OBJECT but was END_DOCUMENT at line 1 column 37
내 ReportAnalysis (문자열이 긴 36 개 문자입니다) :
public class AnalysisReport {
@SerializedName("method")
private String method;
@SerializedName("status")
private String status;
AnalysisReport(){
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getStatus() {
return status;
}
}
코드가 작동하지 않는 이유가 표시되지 않습니다. 나는 = = =
reader.hasNext()
방법은 항상 true
반환 Google
= = = 편집에서 exmaple을 따랐다. 이 메서드는 스트림에서 토큰을 소비하지 않습니다. 그러나 여하튼 여전히 스트림의 끝으로 이동하여 객체를 읽지 못합니다.
문제는 실제로 루프 (facepalm)입니다. 이것은 완벽하게 잘 작동 :
JsonReader reader = new JsonReader(new StringReader(line));
reader.setLenient(true); //accept malformed input
report = gson.fromJson(reader, AnalysisReport.class);
감사
오류없이 Jackson Json 라이브러리와 문자열을 매핑 할 수있었습니다. – grsdev7