简体   繁体   English

在android studio中解析json

[英]parsing json in android studio

I am developing first time in android and i have never used json data before. 我是第一次在android中进行开发,之前从未使用过json数据。 I will develop an application of event calendar of my university. 我将开发我大学的活动日历的应用程序。 We developed web version application in Django and we implement tastypie (restapi) so i need to use this json data for android mobile version. 我们在Django中开发了Web版本应用程序,并且实现了iciouspie(restapi),所以我需要将此json数据用于android移动版本。 My json data is like this : 我的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/"
    }]
}

how can I parse this Json data in android studio? 如何在Android Studio中解析此Json数据?

Using below code you will be able to get Title and Location 使用以下代码,您将可以获得标题位置

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

Use this website to help you view the Json structure better 使用此网站可以帮助您更好地查看Json结构

http://www.jsontree.com/ http://www.jsontree.com/

What you have is a Json Object since it starts and ends with curly braces. 您拥有的是一个Json对象,因为它以花括号开头和结尾。

For example if I had a Json as {"Id":"1"} The Key is "Id" and the value is "1" 例如,如果我有一个Json作为{"Id":"1"} ,则Key为“ Id”,值为“ 1”

A Json object can have a Json inside the value as well(Which is your case) 一个Json对象也可以在值内包含一个Json(这是您的情况)

And example is {"Id":{"Place1":"1", "Place2":"2"}} 例子是{"Id":{"Place1":"1", "Place2":"2"}}

So the Key is "Id" and it has the value "Place1":"1", "Place2":"2" 因此,密钥为"Id" ,其值为"Place1":"1", "Place2":"2"

So the value is also a Json. 因此该值也是Json。

It can get a little messy with Jsons in Jsons. 在Jsons中使用Jsons可能会有些混乱。

Here is a good tutorial on parsing Json 这是解析Json的好教程

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM