This is my json value:
{
"hello": [
{
"names": {
"name": "abc"
}
},
{
"names": {
"name": "def"
}
}
]
}
I tried using XML.toString(new JsonObject())
, and this is what I get:
<hello>
<names>
<name>abc</name>
</names>
</hello>
<hello>
<names>
<name>def</name>
</names>
</hello>
Whereas, the xml I expected was this:
<hello>
<names>
<name>abc</name>
</names>
<names>
<name>def</name>
</names>
</hello>
This unexpected behaviour results in an invalid XML error, since there is no root element now. What am I missing here?
The issue in your JSON code. The []
means array , and by definition array is a set of elements. So the resulting xml code contains a set of hello
elements. Try change your []
with {}
:
{
"hello": {
"names": [
{
"name": "abc"
},
{
"name": "def"
}
]
}
}
Just tried it and got the exact output you're looking for:
<hello>
<names>
<name>abc</name>
</names>
<names>
<name>def</name>
</names>
</hello>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.