2011-05-31 137 views
0

首先感謝Pentium10的this answer!它讓我更進了一步。需要幫助從JSONArray中提取數據

我有在PHP

echo (json_encode($output)); 

產生這個輸出

// linebreaks only for readability, they do not exist in the response 
[["Fitch's Chemist","731 Hay St","(08) 9321 6411"], 
["Ferrara Karaoke Bar","67 Milligan Street","(08) 9481 1909"], 
["Target: Perth","712-720 Hay St","(08) 9327 3700"], 
["C Restaurant","44 St Georges Tce","(08) 9220 8333"], 
["Celona Joe Tailors","146 Murray St","(08) 9325 8274"], 
["Fashion In Colour","Shop 2, 138 Barrack St","(08) 9218 8233"], 
["Mainpeak","858 Hay St","(08) 9322 9044"], 
["Fj Storen Painting Contractors","34 Queen St","(08) 9486 9292"], 
["Financial Pathfinders","Level 4\/81 St Georges Tce","(08) 9213 9699"], 
["Seasons of Perth","37 Pier St","(08) 9325 7655"], 
["David Jones","622 Hay St","(08) 9210 4000"], 
["Pharmacity Chemist Supermart","717 Hay St","(08) 9322 6921"], 
["Austcare","10 Pier St","(08) 9325 9330"], 
["Western Australia","8 Pier St","(08) 9261 6222"], 
["Oceanic Cruises","5 Barrack","(08) 9325 1191"]] 

此輸出填充如下列表陣列產生一個JSONArray;

list(0)["Fitch's Chemist","731 Hay St","(08) 9321 6411"] 
list(1)["Ferrara Karaoke Bar","67 Milligan Street","(08) 9481 1909"] 

我現在需要做的是進一步提取這使「」封閉的數據存儲在三個不同的陣列

try { 
     JSONArray jsonArray = new JSONArray(response); 
     List<String> list = new ArrayList<String>(); 
     for (int i=0; i<jsonArray.length(); i++) { 
      list.add(jsonArray.getString(i)); 
      JSONArray jsonList = new JSONArray(list); 
      BusinessName.add(jsonList.getString(0)); 
      Address.add(jsonList.getString(1)); 
      TelNo.add(jsonList.getString(2)); 
     } 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 

感謝馬丁

+0

你能顯示打印列表(0)的代碼嗎......你粘貼? – WarrenFaith 2011-05-31 11:40:06

+0

System.out.println(list(i)); – martincm 2011-05-31 12:03:14

+0

是否將代碼添加到您的問題中?你應該有點溝通:) – WarrenFaith 2011-05-31 12:26:50

回答

0

編輯:我想你的榜樣,代碼幾乎可以工作,但有一點小錯誤。您不應使用list獲取JSONArray jsonList

此作品(測試):

// this string is just used for testing, use your response... 
String json = "[[\"Fitch's Chemist\",\"731 Hay St\",\"(08) 9321 6411\"]," 
      + "[\"Ferrara Karaoke Bar\",\"67 Milligan Street\",\"(08) 9481 1909\"]," 
      + "[\"Target: Perth\",\"712-720 Hay St\",\"(08) 9327 3700\"]," 
      + "[\"C Restaurant\",\"44 St Georges Tce\",\"(08) 9220 8333\"]," 
      + "[\"Celona Joe Tailors\",\"146 Murray St\",\"(08) 9325 8274\"]," 
      + "[\"Fashion In Colour\",\"Shop 2, 138 Barrack St\",\"(08) 9218 8233\"]," 
      + "[\"Mainpeak\",\"858 Hay St\",\"(08) 9322 9044\"]," 
      + "[\"Fj Storen Painting Contractors\",\"34 Queen St\",\"(08) 9486 9292\"]," 
      + "[\"Financial Pathfinders\",\"Level 4/81 St Georges Tce\",\"(08) 9213 9699\"]," 
      + "[\"Seasons of Perth\",\"37 Pier St\",\"(08) 9325 7655\"]," 
      + "[\"David Jones\",\"622 Hay St\",\"(08) 9210 4000\"]," 
      + "[\"Pharmacity Chemist Supermart\",\"717 Hay St\",\"(08) 9322 6921\"]," 
      + "[\"Austcare\",\"10 Pier St\",\"(08) 9325 9330\"]," 
      + "[\"Western Australia\",\"8 Pier St\",\"(08) 9261 6222\"]," 
      + "[\"Oceanic Cruises\",\"5 Barrack\",\"(08) 9325 1191\"]]"; 

try { 
    JSONArray jsonArray = new JSONArray(json); 
    List<String> list = new ArrayList<String>(); 
    for (int i = 0; i < jsonArray.length(); i++) { 
     list.add(jsonArray.getString(i)); 
     // thats the correct line: 
     JSONArray jsonList = new JSONArray(jsonArray.getString(i)); 
     // Used for debug/test, use your own objects BusinessName, Address and TelNo 
     System.out.println(jsonList.getString(0) + ":" + jsonList.getString(1) + ":" + jsonList.getString(2)); 
    } 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

@WarrenFaith對不起,我不遵循這將如何幫助 – martincm 2011-05-31 12:30:40

+0

它只是迭代通過json數組。 – Hades 2011-05-31 12:37:01

+0

@martincm更新了我的答案。此代碼經過測試... – WarrenFaith 2011-05-31 13:17:23

1

比方說,你有一個JSONArray變量稱爲jsonArray,它包含您的數據。

提取數據,你需要做什麼用:

jsonArray.getJSONObject(INT指數)(返回一個JSONObject) - 使用來獲取對象 jsonArray.getJSONArray(INT指數)內的對象(返回一個JSONArray) - 用於獲得數組中的數組

其餘的是自解釋的。

jsonArray.get(INT指數)(返回一個Object) jsonArray.getBoolean(INT指數)(返回一個布爾) jsonArray.getDouble(INT指數)(返回Double) jsonArray.getInt(INT指數) (返回一個整數) jsonArray.getLong(int index)(返回一個Long) jsonArray.getString(int index)(返回一個字符串)

+0

謝謝DallaRosa你能告訴我哪一個是我的qbject或數組。 – martincm 2011-05-31 12:15:51

+0

你在那裏有一個數組的數組。 您使用getJSONArray(number)來獲取內部數組,然後在第二個數組上使用getString(number)來獲取每個值 – DallaRosa 2011-05-31 18:19:43