gpt4 book ai didi

java - 意外的输入结束 : expected close marker

转载 作者:行者123 更新时间:2023-12-02 09:41:51 34 4
gpt4 key购买 nike

我尝试使用 Jackson v2.9.9 Streaming API 解析 JSON,并得到以下堆栈跟踪:

com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (StringReader); line: 1, column: 1])
at [Source: (StringReader); line: 1, column: 8001]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportInvalidEOF(ParserMinimalBase.java:618)
at com.fasterxml.jackson.core.base.ParserBase._handleEOF(ParserBase.java:485)
at com.fasterxml.jackson.core.base.ParserBase._eofAsNextChar(ParserBase.java:497)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._skipWSOrEnd(ReaderBasedJsonParser.java:2340)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:656)

我已将 Jackson 降级到 2.8,但仍然收到错误,验证输入 JSON 是否有效,并对代码进行 null 检查。

JSON:

{"name":"name", "score":100, "output":[],"images":[],"description":"This is a description"}

代码:

try {
JsonFactory factory = new JsonFactory();
String jsonString = "{\"name\":\"name\", \"score\":100, \"output\":[],\"images\":[],\"description\":\"This is a description\"}";
JsonParser parser = factory.createParser(jsonString);
JsonToken nextToken = parser.nextToken();
while (nextToken != null) {

JsonToken token = parser.getCurrentToken();

if (parser.getCurrentName() != null) {
System.out.println("Current Token 1: " + parser.getCurrentName());
}

if (token.equals(JsonToken.FIELD_NAME)) {
String fieldName = parser.getCurrentName();
parser.nextToken();
if (parser.getCurrentName() != null) {
System.out.println("Current Token 2: " + parser.getCurrentName());
}
while (parser.nextToken() != JsonToken.END_OBJECT) {
parser.nextToken();
if (parser.getCurrentName().equals("name")) {
System.out.println("Name: " + parser.getValueAsString());
}
}

}
parser.close();
}
} catch (IOException ex) {
Logger.getLogger(NewIssueExporter.class.getName()).log(Level.SEVERE, null, ex);
}

第 4 行出错:

while(nextToken != null) {

我希望打印所有调试消息,但在出现错误后不会打印。

最佳答案

当您解析有效的 JSON 时,您需要像考虑状态机一样思考它。您找到了名称 - 这意味着:下一个将是值,等等。您可以将代码简化为:

JsonFactory factory = new JsonFactory();
String jsonString = "{\"name\":\"name\", \"score\":100, \"output\":[1,2],\"images\":[\"image1\",\"image2\"],\"description\":\"This is a description\"}";
JsonParser parser = factory.createParser(jsonString);
JsonToken token;
while ((token = parser.nextToken()) != JsonToken.END_OBJECT) {
if (token == JsonToken.FIELD_NAME) {
// Found name, lets read value
System.out.print(parser.getCurrentName() + " = ");
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println(parser.getValueAsString());
} else if (token == JsonToken.VALUE_NUMBER_INT) {
System.out.println(parser.getValueAsInt());
} else if (token == JsonToken.START_ARRAY) {
// Found start array, lets read it all
System.out.print("[");
while (parser.nextToken() != JsonToken.END_ARRAY) {
System.out.print(parser.getText() + ", ");
}
System.out.println("]");
}
}
}
parser.close();

另请参阅:

关于java - 意外的输入结束 : expected close marker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57013343/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com