I am trying to disable the use of MOXy json with the following structure:
[ { "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" }, ... ] ]
The first element of the array is an object, and the second is another array of complex elements. I really searched here in SO and the MOXy documentation for a simple example without any success.
My best attempt to map a json document to JAVA classes is as follows. The root class is CountryDataResponse (getters and seters ommited):
@XmlRootElement @XmlType(propOrder ={"paginationInfo", "dataArray"}) public class CountryDataResponse { private DataArray dataArray; private PaginationInfo paginationInfo; }
(I see this happen because it is not an array, but I am completely lost.)
The PaginationInfo class models the first element of the root array, and the DataArray class wraps the second element, which is an element of the Array of Data class. In addition, I created the Indicator and Country classes for complex types within each Data element.
Main classes (indicator and country are highlighted):
@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; }
Now debug the following code:
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); }
The res object (of the JAXBElement class) has a value of type ArrayList . the first element of the array is an object of the CountryDataResponse class (it must be PaginationInfo ), the second is another ArrayList with the elements of the CountryDataResponse class (they must Data ).
Can someone help me please, or is it just json garbled and it cannot be automatically unrouted correctly?
Thanks in advance.