意外的输入结束:预期的结束标记

时间:2019-07-12 19:56:59

标签: java json jackson

我正在尝试使用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有效,并对代码进行了空检查。

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) {

我希望所有调试消息都能打印出来,但不会在出错后显示。

1 个答案:

答案 0 :(得分:0)

当您解析有效的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();

另请参阅: