我有以下Json字符串
[{"tipError":"1","desError":"Existen diferencias en los montos de Solicitudes vs Operaciones Solicitadas"},{"tipError":"5","desError":"Las Ventas Totales deben ser mayor"}]
[当我收到该JSON时,我正在尝试将其映射到对象的ArrayList。我尝试了here中的三个选项以及stack overflow]中的其他一些示例
这是我目前拥有的
String errors = "[{\"tipError\":\"1\",\"desError\":\"Existen diferencias en los montos de Solicitudes vs Operaciones Solicitadas\"},{\"tipError\":\"5\",\"desError\":\"Las Ventas Totales deben ser mayor\"}]"; List<ProcedureErrorDTO> error = mapper.readValue(errors, new TypeReference<List<ProcedureErrorDTO>>() {});
我的ProcedureErrorDTO类
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({ "tipError", "desError" }) public class ProcedureErrorDTO { @JsonProperty("tipError") private String errorNumber; @JsonProperty("desError") private String errorDescription; public ProcedureErrorDTO() {} public ProcedureErrorDTO(String errorNumber, String errorDescription) { super(); this.errorNumber = errorNumber; this.errorDescription = errorDescription; } @JsonProperty("tipError") public String getErrorNumber() { return errorNumber; } @JsonProperty("tipError") public void setErrorNumber(String errorNumber) { this.errorNumber = errorNumber; } @JsonProperty("desError") public String getErrorDescription() { return errorDescription; } @JsonProperty("desError") public void setError(String errorDescription) { this.errorDescription = errorDescription; } }
我的错误
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: {"tipError":"1","desError":"Existen diferencias en los montos de Solicitudes de BBVA en Pasivos vs Operaciones Solicitadas"}; line: 1, column: 1]
我有以下Json字符串[{“ tipError”:“ 1”,“ desError”:“在存在与否之间存在差异,},{” tipError“:” 5“,” desError“:” Las Ventas Totales deben ser ...
下面的代码工作正常。我什么都没改变。我只将所有内容放在一个java文件下,以便我可以运行它。您是否使用任何特定的映射器配置?
import java.io.IOException;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class App {
public static void main(String[] argv) throws JsonParseException, JsonMappingException, IOException {
String errors = "[{\"tipError\":\"1\",\"desError\":\"Existen diferencias en los montos de Solicitudes vs Operaciones Solicitadas\"},{\"tipError\":\"5\",\"desError\":\"Las Ventas Totales deben ser mayor\"}]";
ObjectMapper mapper = new ObjectMapper();
List<ProcedureErrorDTO> error = mapper.readValue(errors, new TypeReference<List<ProcedureErrorDTO>>() {});
System.out.println(error.size());
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"tipError",
"desError"
})
class ProcedureErrorDTO {
@JsonProperty("tipError")
private String errorNumber;
@JsonProperty("desError")
private String errorDescription;
public ProcedureErrorDTO() {}
public ProcedureErrorDTO(String errorNumber, String errorDescription) {
super();
this.errorNumber = errorNumber;
this.errorDescription = errorDescription;
}
@JsonProperty("tipError")
public String getErrorNumber() {
return errorNumber;
}
@JsonProperty("tipError")
public void setErrorNumber(String errorNumber) {
this.errorNumber = errorNumber;
}
@JsonProperty("desError")
public String getErrorDescription() {
return errorDescription;
}
@JsonProperty("desError")
public void setError(String errorDescription) {
this.errorDescription = errorDescription;
}
}