【问题标题】:How to get this data in JSON in android如何在android中以JSON格式获取这些数据
【发布时间】:2014-12-26 03:38:50
【问题描述】:

我有这个 URL 的数据
http://api.clicky.com/api/stats/4?site_id=32020&sitekey=71bf7c1681e22468&type=visitors&output=json&json_callback=data%22
以及如何在android中获得这个, 我的问题是如何访问所有这些对象和数组,帮帮我

[
  {
    "type": "visitors",
    "dates": [
      {
        "date": "2014-12-25",
        "items": [
          { "value":"70" }
        ]
      }
    ]
  }
]

【问题讨论】:

  • 要访问这些数据,您必须对给定的 api 链接进行异步调用。然后作为响应,您将获得此 json 数据。通过该响应,您可以获得有意义的数据。

标签: android json


【解决方案1】:
  JSONArray jsonArray = new JSONArray("here is your json string ") ;
    int count = jsonArray.length() ;
    for (int i = 0; i < count; i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i) ;
        String type = jsonObject.optString("type") ;
        JSONArray datesArray = jsonObject.optJSONArray("dates") ;
        int datesCount = datesArray.length() ;
        for(int j = 0; j< datesCount; j++){
           JSONObject dateItem = datesArray.get(j) ;
           String dateStr = dateItem.optString("date");
           JSONArray itemsArray = dateItem.optJSONArray("items");
           for(int f = 0 ; f < itemsArray.length(); f++){
               JSONObject valueJson = itemsArray.get(f);
               String value = valueJson.optString("value");
           }
        }

    }

【讨论】:

  • 你应该试着去理解它~
  • JSONArray jsonArray = new JSONArray("这是你的 json 字符串") ; for String 表示我们正确传递的结果
  • 我现在知道了,感谢您的帮助,但有一些未完成的方法还可以
【解决方案2】:

我认为,您可以使用 GSON 库来解析您的 JSON。只需为 JSON 创建类并使用以下代码:

new Gson().fromJson(json, MyGsonClass.class);

【讨论】:

    猜你喜欢
    • 2011-08-18
    • 1970-01-01
    • 2014-01-26
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 2017-07-31
    相关资源
    最近更新 更多