I have XML which can be of any structure like below. Therefore there is no POJO class for instantiation. As you can see I have nested arrays (in this case reservations
is an array of two reservation
and every reservation
have simple elements and arrays of rooms
- but that is only example - there may be more arrays and nested arrays).
<reservations>
<reservation>
<id>1318504</id>
<add_date>2020-12-10 12:48:09</add_date>
<rooms>
<room>
<id>28902</id>
<floor>2</floor>
</room>
<room>
<id>28903</id>
<floor>3</floor>
</room>
</rooms>
</reservation >
<reservation>
<id>1318501</id>
<add_date>2021-05-07 07:47:05</add_date>
<rooms>
<room>
<id>5</id>
<floor>25</floor>
</room>
<room>
<id>6</id>
</room>
</rooms>
</reservation>
</reservations>
I need to convert it to Json as:
{
"reservations":[
{
"id":"1318504",
"add_date":"2020-12-10 12:48:09",
"rooms":[
{
"id":"28902",
"floor":2
},
{
"id":"28903",
"floor":3
}
]
},
{
"id":"1318501",
"add_date":"2021-05-07 07:47:05",
"rooms":[
{
"id":"5",
"floor":25
},
{
"id":"6"
}
]
}
]
}
I have read lots of topic about converting XML to Json. I tried something like:
json.org
JSONObject xmlJSONObj = XML.toJSONObject(xmlString);
jackson
JsonNode node = new XmlMapper().readTree(xmlContent);
underscore-java
String jsonString = U.xmlToJson(xmlContent)
and so on. Bu the result, in best case is like:
{
"reservations":{
"reservation":[
{
"rooms":{
"room":[
{
"id":28902,
"floor":2
},
{
"id":28903,
"floor":3
}
]
},
"add_date":"2020-12-10 12:48:09",
"id":1318504
},
{
"rooms":{
"room":[
{
"id":5,
"floor":25
},
{
"id":6
}
]
},
"add_date":"2021-05-07 07:47:05",
"id":1318501
}
]
}
}
I don't need nodes like reservation
and room
. Could you help me how can I solve this problem? I can manipulate with xml or json, but the form without these nodes is my target.
You may be able to use json:Array="true" in the array - and the JsonConvert will interpret it correctly.
https://www.newtonsoft.com/json/help/html/ConvertXmlToJsonForceArray.htm