gpt4 book ai didi

java - 由于输入结束 jackson 解析器,没有要映射的内容

转载 作者:IT老高 更新时间:2023-10-28 12:44:37 25 4
gpt4 key购买 nike

我从服务器收到此响应 {"status":"true","msg":"success"}

我正在尝试使用 Jackson 解析器库解析这个 json 字符串,但不知何故我正面临映射异常声明

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: java.io.StringReader@421ea4c0; line: 1, column: 1]

为什么会出现这种异常?

如何理解是什么导致了这个异常?

我正在尝试使用以下方式解析:

StatusResponses loginValidator = null;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.AUTO_CLOSE_SOURCE, true);

try {
String res = result.getResponseAsString();//{"status":"true","msg":"success"}
loginValidator = objectMapper.readValue(result.getResponseAsString(), StatusResponses.class);
} catch (Exception e) {
e.printStackTrace();
}

StatusResponse 类

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "status","msg" })
public class StatusResponses {

@JsonProperty("status")
public String getStatus() {
return status;
}

@JsonProperty("status")
public void setStatus(String status) {
this.status = status;
}

@JsonProperty("msg")
public String getMessage() {
return message;
}

@JsonProperty("msg")
public void setMessage(String message) {
this.message = message;
}

@JsonProperty("status")
private String status;

@JsonProperty("msg")
private String message;

private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonGetter
public Map<String, Object> getAdditionalProperties() {
return additionalProperties;
}

@JsonSetter
public void setAdditionalProperties(Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
}
}

最佳答案

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.ObjectMapper;

StatusResponses loginValidator = null;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.AUTO_CLOSE_SOURCE, true);

try {
String res = result.getResponseAsString();//{"status":"true","msg":"success"}
loginValidator = objectMapper.readValue(res, StatusResponses.class);//replaced result.getResponseAsString() with res
} catch (Exception e) {
e.printStackTrace();
}

不知道它是如何工作的以及为什么工作? :(但它有效

关于java - 由于输入结束 jackson 解析器,没有要映射的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26925058/

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