【问题标题】:Need help extracting data from JSONArray需要帮助从 JSONArray 中提取数据
【发布时间】:2011-05-31 11:36:39
【问题描述】:

首先感谢 Pentium10 提供this answer!它让我更进一步。

我有一个由 php 生成的 JSONArray

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

谢谢马丁

【问题讨论】:

  • 你能显示打印列表(0)的代码吗...你粘贴的?
  • 您添加到问题中的代码是否有效?你应该多交流一点:)
  • 对不起,你是对的。请耐心等待这是我的第一篇文章。不,代码不起作用,它会抛出 JSON 错误 int 超出范围

标签: android json arraylist


【解决方案1】:

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

要提取数据,你需要做什么:

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

其余的都是自解释的。

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

【讨论】:

  • 谢谢 DallaRosa 你能告诉我我有哪一个是 qbject 还是一个数组。
  • 你有一个数组数组。您使用 getJSONArray(number) 来获取内部数组,然后在第二个数组上使用 getString(number) 来获取每个值
【解决方案2】:

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

【讨论】:

  • @WarrenFaith 抱歉,我不知道这会有什么帮助
  • 只是遍历json数组。
  • @WarrenFaith 谢谢,我知道我哪里出错了。我试图从整个列表而不是列表(i)中读取。在第一次迭代 list(0) 存在但 list(1) 不因此错误。使用您的代码,我不需要 list.add(jsonArray.getString(i));
猜你喜欢
  • 1970-01-01
  • 2016-07-05
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
相关资源
最近更新 更多