Jackson 无法解析 json、returns NPE

Jackson can't parse json, returns NPE

我正在尝试解析看起来像那样的 json(steam 网络聊天)(我更改了响应,因为我不想显示数据):

/**/({
"pollid": 00,
"messages": [
    {
        "type": "personastate",
        "timestamp": 0000000000,
        "utc_timestamp": 000000000,
        "steamid_from": "000000000000",
        "status_flags": 0000000,
        "persona_state": 0,
        "persona_name": "asd"
    }
]
,
"messagelast": 00,
"timestamp": 0000000000,
"utc_timestamp": 000000000000,
"messagebase": 00,
"sectimeout": 0,
"error": "OK"
})

我的解析 class 看起来像这样:

package jsonRequest;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

public class NewMessageJson {

public Integer poollid;
private String lastMessageId;
private String error;
private String messageBase;

public NewMessageJson(String response) {
response = response.substring(response.indexOf("{"),
    response.indexOf("}") + 1); // cut off comment block


JsonFactory factory = new JsonFactory();
JsonParser jp = null;
try {
    jp = factory.createJsonParser(response);
} catch (JsonParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
try {

    if (jp.nextToken() != JsonToken.START_OBJECT) {
    throw new IOException("Server didn't return any data");
    }

    while (jp.nextToken() != JsonToken.END_OBJECT) {
    String fieldName = jp.getCurrentName();
    jp.nextToken();
    if (fieldName.equals("messagelast")) {
        setLastMessageId(jp.getText());
    } else if (fieldName.equals("pollid")) {
        setPoollid(jp.getIntValue());
    } else if (fieldName.equals("messagebase")) {
        setMessageBase(jp.getText());
    } else if (fieldName.equals("error")) {
        setError(jp.getText());
    }
    }
    jp.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (NullPointerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {
    jp.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

public Integer getPoollid() {
return poollid;
}

public void setPoollid(Integer poollid) {
this.poollid = poollid;
}

public String getLastMessageId() {
return lastMessageId;
}

public void setLastMessageId(String lastMessageId) {
this.lastMessageId = lastMessageId;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

public String getMessageBase() {
return messageBase;
}

public void setMessageBase(String messageBase) {
this.messageBase = messageBase;
}

}

当谈到这条线时

if (fieldName.equals("messagelast")) {

它崩溃并且 returns NPE。 我还有另外 3 个 classes 看起来和这个一模一样,一切都很完美。

我很确定您获得 NPE 的原因是因为您最初将 JsonParser jp 实例化为 null。您将它分配给 try 块中的 factory.createJsonParser(response),但除了打印堆栈跟踪之外,不以任何方式处理错误。如果执行 factory.createJsonParser(response) 时出错,您需要确保没有其他运行。

我建议将您的代码更改为:

...

JsonFactory factory = new JsonFactory();
JsonParser jp = null;
try {
    jp = factory.createJsonParser(response);
} catch (JsonParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    System.out.println("There was an error while setting jp to factory.createJsonParser(response). Error message is: " + e1.getMessage());
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    System.out.println("There was an error while setting jp to factory.createJsonParser(response). Error message is: " + e1.getMessage());
}

if(jp != null) {
try {

    if (jp.nextToken() != JsonToken.START_OBJECT) {
    throw new IOException("Server didn't return any data");
    }

    while (jp.nextToken() != JsonToken.END_OBJECT) {
    String fieldName = jp.getCurrentName();
    jp.nextToken();
    if (fieldName.equals("messagelast")) {
        setLastMessageId(jp.getText());
    } else if (fieldName.equals("pollid")) {
        setPoollid(jp.getIntValue());
    } else if (fieldName.equals("messagebase")) {
        setMessageBase(jp.getText());
    } else if (fieldName.equals("error")) {
        setError(jp.getText());
    }
    }
    jp.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (NullPointerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {
    jp.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}
}

...

这样,你就可以避免所有的NPE!

编辑:您还应该实施 peeskillet 建议的内容