需要帮助从JSONArray中提取数据



首先感谢Pentium10的回答!它让我更进一步。

我有一个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"]]

这将输出如下所示填充的列表数组;

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();
      }

感谢Martin

假设您有一个名为JSONArray的JSONArray变量,它包含您的数据。

要提取数据,您需要使用

getJSONObject(int index)(返回一个JSONObject)-用于获取对象内部的对象getJSONArray(int index)(返回一个jsonArray)-用于获取数组中的数组

其余部分不言自明。

get(int index)(返回一个Object)getBoolean(int index)(返回布尔值)getDouble(int index)(返回一个Double)jsonArray.getInt(int index)(返回Integer)getLong(int index)(返回一个Long)jsonArray.getString(int index)(返回字符串)

edit:我尝试了您的示例,代码几乎可以工作,但有一个小错误。您不应该使用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();
}

最新更新