【问题标题】:parsing json in android studio在android studio中解析json
【发布时间】:2015-07-23 10:49:15
【问题描述】:

我是第一次在 android 中开发,我以前从未使用过 json 数据。我将开发我大学的活动日历的应用程序。我们在 Django 中开发了 web 版本的应用程序,我们实现了 sweetpie (restapi),所以我需要将此 json 数据用于 android 移动版本。我的 json 数据是这样的:

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 5
    },
    "objects": [{
        "Location": "Z011",
        "Notes": "asdf",
        "Title": "Literature Talking",
        "id": 3,
        "resource_uri": "/api/v1/Events/3/"
    }, {
        "Location": "Batı Kampüsü, Sinema Salonua",
        "Notes": "sd",
        "Title": "TARİHÇE KONFERANSLARI SERİSİ 25",
        "id": 4,
        "resource_uri": "/api/v1/Events/4/"
    }, {
        "Location": "in Campus",
        "Notes": "afafdf",
        "Title": "Self-Assessment Project",
        "id": 5,
        "resource_uri": "/api/v1/Events/5/"
    }, {
        "Location": "Kütüphane",
        "Notes": "fs",
        "Title": "51.Kütüphane  Haftası",
        "id": 6,
        "resource_uri": "/api/v1/Events/6/"
    }]
}

如何在 android studio 中解析这个 Json 数据?

【问题讨论】:

标签: java android json django


【解决方案1】:

使用以下代码,您将能够获得 TitleLocation

    JSONObject obj=new JSONObject(response);//This is response from webservice
    String totalCount = obj.getJSONObject("meta").getString("total_count"); //for getting total_count
    JSONArray json_array = obj.getJSONArray("objects");
    for(int j=0;j<json_array.length();j++) {
        String title = json_array.getJSONObject(j).getString("Title");
        String location= json_array.getJSONObject(j).getString("Location");
    }

【讨论】:

    【解决方案2】:

    使用本网站帮助您更好地查看 Json 结构

    http://www.jsontree.com/

    您拥有的是一个 Json 对象,因为它以花括号开头和结尾。

    例如,如果我有一个 Json 为 {"Id":"1"} Key为“Id”,值为“1”

    一个 Json 对象也可以在值中包含一个 Json(这是你的情况)

    例如{"Id":{"Place1":"1", "Place2":"2"}}

    所以密钥是"Id",它的值是"Place1":"1", "Place2":"2"

    所以值也是一个Json。

    Jsons 中的 Jsons 可能会有点混乱。

    这里有一个很好的解析Json的教程

    http://www.tutorialspoint.com/android/android_json_parser.htm

    【讨论】:

      猜你喜欢
      • 2016-04-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 2021-10-30
      • 2015-10-19
      相关资源
      最近更新 更多