I am trying to convert the below JSON into a Java object using ObjectMapper class in jackson api but i am getting an error. How my java class should look like?
我试图在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"
}]
If i have the below json i am able to convert it to java object.
如果我有以下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.
}
Json:
JSON:
{
"saleValue": 100,
"priceEventTypeCode": 1,
"startDate": "2016-03-23T18:00:00.0Z",
"updateStoredValue": true,
"autoRound": false,
"priceChangeNumber": "tkt"
}
1 个解决方案
#1
2
ObjectMapper support below method. Use them :)
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));
#1
2
ObjectMapper support below method. Use them :)
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));