杰克逊Json List里面的对象,如何获取bean?

时间:2017-06-11 12:41:01

标签: java json

我很难使用Jackson将Json String转换为java中的Object。

模型

public class PPDResult {


    int Result;
    String ResultMessage;

    List<PPDObj> LoanInfos;
}

public class PPDObj {

    private int ListingId;

    private String Title;

    private String CreditCode;

    private BigDecimal Amount;

    private Double Rate;

    private int Months;

    private int PayWay;

    private BigDecimal RemainFunding;
}

数据:

{
    "LoanInfos": [
        {
            "ListingId": 52233312,
            "Title": "xxxxxxx",
            "CreditCode": "D",
            "Amount": 787,
            "Rate": 22,
            "Months": 6,
            "PayWay": 0,
            "RemainFunding": 387
        },
        {
            "ListingId": 52233362,
            "Title": "xxxxxxxxx",
            "CreditCode": "B",
            "Amount": 10000,
            "Rate": 18,
            "Months": 6,
            "PayWay": 0,
            "RemainFunding": 7695
        }
    ],
    "Result": 1,
    "ResultMessage": "success",
    "ResultCode": null
}

检索代码:

String resultStr = new BufferedReader(inputStreamReader).readLine();        

pPDResult = mapper.readValue(resultStr, PPDResult.class);

错误:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "LoanInfos" (class ppd.pojo.PPDResult), not marked as ignorable (3 known properties: , "resultMessage", "result", "loanInfos"])
 at [Source: xxxxxxxxx; line: 1, column: 15] (through reference chain: ppd.pojo.PPDResult["LoanInfos"])

问题:

有什么问题? 如何编写正确的代码?

我hava参考 Jackson Json List inside object 但还没有解决 enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

它的决心。 谢谢@ cricket_007

我尝试了更改模型,将@JsonProperty添加到字段中,如下所示:

public class PPDObj {

    @JsonProperty("ListingId")
    private int ListingId;
    @JsonProperty("Title")
    private String Title;
    @JsonProperty("CreditCode")
    private String CreditCode;
    @JsonProperty("Amount")
    private BigDecimal Amount;
    @JsonProperty("Rate")
    private Double Rate;
    @JsonProperty("Months")
    private int Months;
    @JsonProperty("PayWay")
    private int PayWay;
    @JsonProperty("RemainFunding")
    private BigDecimal RemainFunding;

}
public class PPDResult {

    @JsonProperty("Result")
    int Result;
    @JsonProperty("ResultMessage")
    String ResultMessage;
    @JsonProperty("ResultCode")
    String ResultCode;
    @JsonProperty("LoanInfos")
    List<PPDObj> LoanInfos;
}

谢谢!