转换嵌套XML到JSON
问题描述:
我需要编写XML转换成JSON库。我一直在尝试使用帮助从这样的一个答案,我能做到这一点:XML的转换嵌套XML到JSON
例子:
<website>
<created-at type="datetime"> 2010-02-17T14:36:26-08:00</created-at>
<id type=
"integer"> 12</id>
<primary-host-id type="integer" nil="true"></primary-host-id>
<suspended type="boolean"> false</suspended>
<hosts type="array">
<host>
<id type="integer"> 12</id>
<name> example.viviti.com</name>
</host>
<host>
<id type="integer"> 12</id>
<name> example.viviti.com</name>
</host>
</hosts>
<ip-address> 127.0.0.1</ip-address>
</website>
所以,我写的东西产生此JSON代码,
{ip-address= 127.0.0.1, hosts={host=[{name= example.viviti.com, id= 12}, {name= example.viviti.com, id= 12}]}, created-at= 2010-02-17T14:36:26-08:00, id= 12, primary-host-id=, suspended= false}
它正在“主机”为数组,但我需要的是“主机”是一个数组。 因此,预期的JSON会是这样的:
{
"website":{
"created-at":"2010-02-17T14:36:26-08:00",
"id":"12",
"suspended":"false",
"ip-primary-host-id":"",
"ip-address":"127.0.0.1",
"hosts":[
{
"host":{
"name":"example.viviti.com",
"id":"12"
}
},
{
"host":{
"name":"example1.viviti.com",
"id":"13"
}
}
]
}
}
这是我的现有代码:
public static void main(String[] args) throws Exception
{
XStream magicApi = new XStream();
magicApi.registerConverter(new MapEntryConverter());
magicApi.alias("website", Map.class);
Map extractedMap = (Map) magicApi.fromXML(RESPONSE);
System.out.println(extractedMap);
}
public static class MapEntryConverter implements Converter
{
public boolean canConvert(Class clazz)
{
return AbstractMap.class.isAssignableFrom(clazz);
}
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context)
{
AbstractMap map = (AbstractMap) value;
for (Object obj : map.entrySet())
{
Map.Entry entry = (Map.Entry) obj;
writer.startNode(entry.getKey().toString());
Object val = entry.getValue();
if (null != val)
{
writer.setValue(val.toString());
}
writer.endNode();
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
{
Map<String, Object> map = new HashMap<String, Object>();
while (reader.hasMoreChildren())
{
reader.moveDown();
String key = reader.getNodeName();
if(reader.hasMoreChildren())
{
// reader.moveDown();
Object interim = unmarshal(reader, context);
if(!map.containsKey(key))
{
map.put(key, interim);
}
else
{
List list = new ArrayList();
list.add(map.get(key));
list.add(interim);
map.put(key,list);
}
// reader.moveUp();
}
else
{
String value = reader.getValue();
map.put(key, value);
}
reader.moveUp();
}
return map;
}
}
而且,我不想在JSON的XML命名空间。 欣赏帮助。
答
你为什么不使用JSONObject
。使用这个对象来生成JSON要简单得多。它会为你节省很多很多的LOC。
public class Main {
public static void main(String[] args) {
try {
JSONObject xmlJSONObj = XML.toJSONObject(YOUR_XML_STRING);
System.out.println(xmlJSONObj.toString());
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
输出
{
"website": {
"created-at": {
"-type": "datetime",
"#text": " 2010-02-17T14:36:26-08:00"
},
"id": {
"-type": "integer",
"#text": " 12"
},
"primary-host-id": {
"-type": "integer",
"-nil": "true"
},
"suspended": {
"-type": "boolean",
"#text": " false"
},
"hosts": {
"-type": "array",
"host": [
{
"id": {
"-type": "integer",
"#text": " 12"
},
"name": " example.viviti.com"
},
{
"id": {
"-type": "integer",
"#text": " 12"
},
"name": " example.viviti.com"
}
]
},
"ip-address": " 127.0.0.1"
}
}
这将导致类似json的,因为我现在得到。另外,我想删除它没有的名称空间。 – doctore
您提到的两个JSON都是无效的。你可以解析它们[在这里](http://codebeautify.org/jsonviewer)并亲自查看结果。 –
JSON可能不正确(一些paranthesis可能是额外的或缺失的),但那不是我关心的问题。我正在谈论内在的因素。我也会发布正确的json。 – doctore