2015-11-01 75 views
2

這是我的JSON值:XML到JSON - 轉換列表時出現意外的行爲?

{ 
    "hello": [ 
     { 
      "names": { 
       "name": "abc" 
      } 
     }, 
     { 
      "names": { 
       "name": "def" 
      } 
     } 
    ] 
} 

我試着用XML.toString(new JsonObject()),這就是我得到:

<hello> 
     <names> 
      <name>abc</name> 
     </names> 
    </hello> 
    <hello> 
     <names> 
      <name>def</name> 
     </names> 
    </hello> 

然而,我期望的XML是這樣的:

<hello> 
     <names> 
      <name>abc</name> 
     </names> 
     <names> 
      <name>def</name> 
     </names> 
    </hello> 

此意外行爲會導致無效的XML錯誤,因爲現在沒有根元素。我在這裏錯過了什麼?

+0

你的意思'XML.toString(新的JSONObject())'? –

+0

發佈你的代碼plz – Rehman

+0

@Rehman:除了XML.toString(新的JSONObject(字符串)),沒有其他重要的代碼片段。 –

回答

3

您的JSON代碼中的問題。 []意味着數組,並根據定義陣列是一個的元素。所以得到的xml代碼包含集合hello元素。試着改變你的[]{}

{ 
    "hello": { 
     "names": [ 
      { 
       "name": "abc" 
      }, 
      { 
       "name": "def" 
      } 
     ] 
    } 
} 

就嘗試過了,得到你正在尋找確切的輸出:

<hello> 
    <names> 
     <name>abc</name> 
    </names> 
    <names> 
     <name>def</name> 
    </names> 
</hello> 
相關問題