需要帮助从JSONArray中提取数据

时间:2011-05-31 11:36:39

标签: android json arraylist

首先感谢Pentium10 for 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"]]

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

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

感谢马丁

2 个答案:

答案 0 :(得分:1)

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

要提取数据,您需要执行以下操作:

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

其余的是自我解释。

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

答案 1 :(得分: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();
}
相关问题