2013-07-17 44 views
0

我有這樣JSON:內容分析錯誤

[ 
    { 

     "topic": "Example1", 
     "ref": { 
      "1": "Example Topic", 
      "2": "Topic" 
     }, 
     "contact": [ 
      { 
       "ref": [ 
        1 
       ], 
       "corresponding": true, 
       "name": "XYZ" 
      }, 
      { 
       "ref": [ 
        1 
       ], 
       "name": "ZXY" 
      }, 
      { 
       "ref": [ 
        1 
       ], 
       "name": "ABC" 
      }, 
      { 
       "ref": [ 
        1, 
        2 
       ], 
       "name":"BCA" 
      } 
     ] , 

     "type": "Presentation" 
    } 
] 

我想解析ref陣列JSON文件。我試過這個。但顯示錯誤。

jsonArray.getJSONObject(index).getJSONArray("ref").getJSONObject(index).toString() 

現在的問題是

1)什麼是解析數組內容的正確方法。

+3

什麼是錯誤? – iamnotmaynard

+1

這個JSON有很多'ref';你想要哪一個? –

+0

@JBNizet「ref」:{ 「1」:「示例主題」, 「2」:「主題」 } – user2579475

回答

1

我解決它通過

JSONObject arJS = jsonArray.getJSONObject(index).getJSONObject("ref"); 
for(int counter = 1 ; jo<=jsonArray.getJSONObject(index).getJSONObject("ref").length();counter++){ 

       String value = arJS.getString(String.valueOf(counter)); 

      } 
3

整個JSON是一個數組(與[開始)。其第一個元素是一個對象(以{開頭)。這個對象有一個屬性「ref」。它的值是一個對象(以{開頭)。

因此,要得到這個對象,需要

jsonArray.getJSONObject(index).getJSONObject("ref")