2015-03-31 59 views
-1

我的json,看起來像下面與JSONArrays工作:我轉換如何ArrayList的<String[]>

{ 
    "tiles":[ 
     { 
     "header":"Manual", 
     "subheader":"Login", 
     "summary1_label":"", 
     "summary2_label":"", 
     "summary3_label":"", 
     "summary4_label":"", 
     "summary1_value":"", 
     "summary2_value":"", 
     "summary3_value":"", 
     "summary4_value":"", 
     "iconUrl":"http://x.x.x.x\/icon1.png" 
     }, 
     { 
     "header":"NewUser", 
     "subheader":"Login", 
     "summary1_label":"", 
     "summary2_label":"", 
     "summary3_label":"", 
     "summary4_label":"", 
     "summary1_value":"", 
     "summary2_value":"", 
     "summary3_value":"", 
     "summary4_value":"", 
     "iconUrl":"http://x.x.x.x.x\/icon2.png" 
     }, 
     { 
     "header":"Facebook", 
     "subheader":"Login", 
     "summary1_label":"", 
     "summary2_label":"", 
     "summary3_label":"", 
     "summary4_label":"", 
     "summary1_value":"", 
     "summary2_value":"", 
     "summary3_value":"", 
     "summary4_value":"", 
     "iconUrl":"http://x.x.x.x/icon3.png" 
     } 
    ] 
} 

我投這是一個JSONObject並將其發送到reformating方法。在該方法中我做了以下

public ArrayList<String[]> formatHttpResponse_SummaryTile(JSONObject json) throws JSONException { 
    ArrayList<String[]> arrayList_summary = new ArrayList<String[]>(); 
    JSONArray tilesArray = json.getJSONArray("tiles"); 
    //JSONObject allTilesData = json.getJSONObject("tiles"); 
    for (int i=0; i<tilesArray.length(); i++) 
    { 
     //JSONObject thisJsonObject = tilesArray.getJSONObject(i); 
     JSONArray thisJsonArray = tilesArray.getJSONArray(i); 
     String[] thisStringArray = new String[thisJsonArray.length()]; 
     for (int j=0; j<thisJsonArray.length(); j++) 
     { 
      thisStringArray[j]=thisJsonArray.getString(j); 
     } 
     arrayList_summary.add(thisStringArray); 
    } 
    return arrayList_summary; 
} 

正如你可以看到我通過我的一個JSONObjects 3個孩子創建一個數組列表,然後循環。現在問題是我想將孩子轉換爲String [] [但是我的代碼錯誤地出現在JSONArray thisJsonArray = tilesArray.getJSONArray(i);,因爲它不是jsonArray。

任何想法如何我可以得到上述兒童成3個字符串數組全部添加到arrayList是否有更高效,直接投我可以做?

+0

你是什麼意思「錯誤」 – Epicblood 2015-03-31 18:57:36

+0

你應該讓你的JSON數據更具可讀性。試試這個[JSON Formatter](http://jsonformatter.curiousconcept.com/) – osayilgan 2015-03-31 18:57:58

+0

道歉,SO編輯器刪除了一些空格。我會 – Fearghal 2015-03-31 18:58:36

回答

0
public ArrayList<String[]> formatHttpResponse_SummaryTile(JSONObject json) throws JSONException { 
      JSONObject thisJsonObject = tilesArray.getJSONObject(i); //create jsonObject to grab values from 
      Iterator<String> iter = thisJsonObject.keys(); 
      String[] thisStringArray = new String[thisJsonObject.length()]; 
      int count=0; //track which element we are on in the dest string[] 
      while (iter.hasNext()) 
      { 
       String key = iter.next(); 
       try 
       { 
        String value = (String)thisJsonObject.get(key); 
        thisStringArray[count] = value; 
       } 
       catch (JSONException e) { 
        // Something went wrong! 
        break; 
       } 
       count++; 
      } 
      arrayList_summary.add(thisStringArray); 
} 

return arrayList_summary; 
} 
+0

對不起,'for'循環被切斷了,它與你的一樣 – 2015-03-31 19:07:17

+0

Il給它一個:) – Fearghal 2015-03-31 19:56:25

+0

我也不確定是否。贊成或接受爲答案將不勝感激:) – 2015-03-31 20:03:11

相關問題