我有以下代码:
import org.json.JSONObject;
import org.json.XML;
public class Xml2Json {
public static void main(String[] args) {
String xmlString = "<users><user name=test1 age=20></user><report sub=eng score=30></report></users>";
JSONObject jsonObject = XML.toJSONObject(xmlString);
System.out.println(jsonObject);
}
}
其输出如下所示:
{
"users": {
"report": {
"sub": "eng",
"score": 30
},
"user": {
"name": "test1",
"age": 20
}
}
}
但是我想删除这个根节点,预期的输出是:
{
"report": {
"sub": "eng",
"score": 30
},
"user": {
"name": "test1",
"age": 20
}
}
有人可以帮助我吗?..预先感谢!
答案 0 :(得分:1)
除了将根节点值分配给jsonObject
jsonObject = (JSONObject) jsonObject.get(jsonObject.keys().next());
System.out.println(jsonObject);
您也可以直接指定根节点的名称
jsonObject = (JSONObject) jsonObject.get("users");
输出:
{"report":{"sub":"eng","score":30},"user":{"name":"test1","age":20}}