【问题标题】:How to read Json data in android [closed]如何在android中读取Json数据[关闭]
【发布时间】:2014-04-14 17:29:08
【问题描述】:

我有如下格式的原始数据:

[
  {
    "id": "1",
    "name": "abc",
    "type": "consumer"
  },
  {
    "id": "2",
    "name": "cdf",
    "type": "consumer"
  },
  {
    "id": "3",
    "name": "jok",
    "type": "owner"
  }
]

请让我知道如何将其转换为 JsonArray 并获取每个值。

【问题讨论】:

  • 之前被问了近 100500 次
  • 搜索论坛比回答新问题更容易!
  • 创建一个虚拟项目,复制并粘贴代码,或者从那里下载代码并自行分析......我相信你会在 40 分钟内得到它。 androidhive.info/2012/01/android-json-parsing-tutorial
  • 你需要知道如何解析json?
  • 服务器上的原始数据在哪里??

标签: android json


【解决方案1】:

这是解析JSON 字符串的最简单方法。我建议您阅读JSON 文档。

但是,这里有一个示例代码。

try {
        String jsonString = new String("[{\"id\": \"1\",\"name\": \"abc\",\"type\": \"consumer\"}]");
        JSONArray jsonArray = new JSONArray(jsonString);
        for(int index = 0;index < jsonArray.length(); index++) {
            JSONObject jsonObject = jsonArray.getJSONObject(index);
            String id = jsonObject.getString("id");
            String name = jsonObject.getString("name");
            String type = jsonObject.getString("type");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

也可以使用 GSON https://code.google.com/p/google-gson/解析

【讨论】:

    猜你喜欢
    • 2012-11-28
    • 2023-04-02
    • 1970-01-01
    • 2021-12-13
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    相关资源
    最近更新 更多