2016-07-22 224 views
0

我需要編寫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命名空間。 欣賞幫助。

回答

0

你爲什麼不使用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" 
    } 
} 

more info...

+0

這將導致類似json的,因爲我現在得到。另外,我想刪除它沒有的名稱空間。 – doctore

+0

您提到的兩個JSON都是無效的。你可以解析它們[在這裏](http://codebeautify.org/jsonviewer)並親自查看結果。 –

+0

JSON可能不正確(一些paranthesis可能是額外的或缺失的),但那不是我關心的問題。我正在談論內在的因素。我也會發布正確的json。 – doctore