我試圖使用MOXY一個JSON具有以下結構來解組:JAXB:解組異質陣列
[
{
"page": 1,
"pages": 1
},
[
{
"indicator": {
"id": "IC.BUS.EASE.XQ",
"value": "Ease of doing business index"
},
"country": {
"id": "1A",
"value": "Arab World"
},
"value": "113.952380952381",
"date": "2014"
},
...
]
]
陣列的第一個元素是一個對象和第二元件是複雜的元件的另一陣列。我真的在SO和MOXY文檔中找到了一個沒有任何成功的類似例子。
我將json文檔映射到JAVA類的最佳嘗試如下。根類是CountryDataResponse(干將& setter方法中省略):
@XmlRootElement
@XmlType(propOrder ={"paginationInfo", "dataArray"})
public class CountryDataResponse {
private DataArray dataArray;
private PaginationInfo paginationInfo;
}
(我可以看到這個是要失敗的,因爲它不是一個數組,但我完全地丟失)
PaginationInfo類爲根數組的第一個元素建模,DataArray類包裝第二個元素,它是一個Data類元素數組。另外,我爲每個Data元素中的複雜類型創建了Indicator和Country類。
主要類別(指標和國家中省略):
@XmlRootElement(name = "paginationInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class PaginationInfo {
private int page;
private int pages;
}
@XmlRootElement(name = "dataArray")
public class DataArray {
List<Data> datas;
}
@XmlRootElement(name="data")
@XmlAccessorType(XmlAccessType.FIELD)
public class Data {
private Indicator indicator;
private Country country;
private String date;
private double value;
}
現在,調試以下代碼:
public static void main(String args[]) {
String test = "[{\"page\": 1,\"pages\": 1,\"per_page\": \"1000\",\"total\": 248},"
+ "["
+ "{\"indicator\": {\"id\": \"NY.GDP.MKTP.CD\",\"value\": \"GDP (current US$)\"},"
+ "\"country\": {\"id\": \"1A\",\"value\": \"Arab World\"},"
+ "\"value\": \"2853079422103.94\","
+ "\"decimal\": \"1\","
+ "\"date\": \"2013\"},"
+ "{\"indicator\": {\"id\": \"NY.GDP.MKTP.CD\",\"value\": \"GDP (current US$)\"},"
+ "\"country\": {\"id\": \"S3\",\"value\": \"Caribbean small states\"},"
+ "\"value\": \"67033118185.1864\","
+ "\"decimal\": \"1\","
+ "\"date\": \"2013\"}"
+ "]]";
JAXBContext jc = JAXBContext.newInstance(CountryDataResponse.class, Country.class, Data.class, DataArray.class, Indicator.class, PaginationInfo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
Object res = unmarshaller.unmarshal(json, CountryDataResponse.class);
}
res對象(類JAXBElement)具有值類型ArrayList。陣列的第一元件是類CountryDataResponse的目的(它應該PaginationInfo),第二是另一個的ArrayList與類CountryDataResponse,太的元件(它們應該是是數據實例)。
任何人都可以幫助我,或者它只是一個畸形的json,它不能自動正確解組?
預先感謝您。
謝謝您的回答。事實上,你的第一個建議是現在編組一個CountryDataResponse的結果,所以它會使用它很有意義:)不幸的是,我不能改變結構:它來自http://api.worldbank.org/國家/指示器/ NY.GDP.MKTP.CD?per_page = 1000&MRV = 1&格式= JSON。 – MrMiyagi
在這種情況下,我會看看爲CountryDataResponse對象創建自定義序列化程序: http://www.baeldung.com/jackson-custom-serialization – robinsio