[英]parsing json in android studio
我是第一次在android中进行开发,之前从未使用过json数据。 我将开发我大学的活动日历的应用程序。 我们在Django中开发了Web版本应用程序,并且实现了iciouspie(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数据?
使用以下代码,您将可以获得标题和位置
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");
}
使用此网站可以帮助您更好地查看Json结构
您拥有的是一个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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.