我必须将xml转换为JSON。我已经以下面的方式实现了
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer
XMLSerializer xmlSerializer = new XMLSerializer();
//xmlSerializer.setForceTopLevelObject(true);
//Above line is comented since i want to skip xml root element
JSON json = xmlSerializer.read(inputXml);
outJSON = json.toString(2);
我的输入xml是
<details>
<name>
<firstName>sir</firstName>
<lastName>John</lastName>
<midddleName>Nash</midddleName>
</name>
<address>24525 ross street</address>
</details>
我的输出JSON是,它可以很好地跳过根元素
{
"name": {
"firstName": "sir",
"lastName": "John",
"midddleName": "Nash"
},
"address": "24525 ross street"
}
但是当我提出请求时
<details>
<name>
<firstName>sir</firstName>
<lastName>John</lastName>
<midddleName>Nash</midddleName>
</name>
</details>
我的JSON回复是
[
{
"firstName": "sir",
"lastName": "John",
"midddleName": "Nash"
}
]
但我需要回复
{
"name": {
"firstName": "sir",
"lastName": "John",
"midddleName": "Nash"
}
}
即使有任何其他API提供此功能,请建议我