0

我正在尝试使用 jackson api 中的 ObjectMapper 类将以下 JSON 转换为 Java 对象,但出现错误。我的 java 类应该是什么样子?

[{
    "saleValue": 100,
    "priceEventTypeCode": 1,
    "startDate": "2016-03-23T18:00:00.0Z",
    "updateStoredValue": true,
    "autoRound": false,
    "priceChangeNumber": "tkt"
},
{
    "saleValue": 100,
    "priceEventTypeCode": 1,
    "startDate": "2016-03-23T18:00:00.0Z",
    "updateStoredValue": true,
    "autoRound": false,
    "priceChangeNumber": "tkt"
}]

如果我有以下 json,我可以将其转换为 java 对象。

public class Pid
{
    private String priceChangeNumber;
    private String startDate;
    private String autoRound;
    private String priceEventTypeCode;
    private String saleValue;
    private String updateStoredValue;

    // getter and setter functions removed here.
}

杰森:

{
    "saleValue": 100,
    "priceEventTypeCode": 1,
    "startDate": "2016-03-23T18:00:00.0Z",
    "updateStoredValue": true,
    "autoRound": false,
    "priceChangeNumber": "tkt"
}
4

1 回答 1

2

ObjectMapper 支持以下方法。使用它们 :)

    //To Array
    Pid[] pidArray = mapper.readValue(json, Pid[].class);

    //To List
    List<Pid> pidList1 = mapper.readValue(json, new TypeReference<List<Pid>>(){});

    //To List Another way
    List<Pid> pidList2 = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Pid.class));
于 2016-02-20T15:09:57.380 回答