android解析嵌套的json对象 [英] android-parsing nested json Objects
本文介绍了android解析嵌套的json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在我的应用程序中实现json数据.但是我不知道如何从服务器获取数据.我正在使用ION库.
i have to implement json data in my application. but i can't get it how to fetch data from server. i am using ION library.
{
"search": [{
"id": "5454003",
"description": "Larger Than Life",
"url": "http://audiojungle.net/item/larger-than-life/5454003",
"type": "item",
"sales": "1469",
"rating": "5",
"item_info": {
"id": "5454003",
"item": "Larger Than Life",
"url": "http://audiojungle.net/item/larger-than-life/5454003",
"user": "pinkzebra",
"thumbnail": "https://0.s3.envato.com/files/67162834/upbeatsongwinner2.jpg",
"sales": "1469",
"rating": "5",
"rating_decimal": "4.96",
"cost": "18.00",
"preview_type": "audio",
"preview_url": "https://0.s3.envato.com/files/94250640/preview.mp3",
"length": "2:35"
}
} ] }
这是我的json数据.我想在我的应用程序中显示成本",用户"等数据.但我不明白.离子代码在这里,
this is my json data. i want to display in my application data like "cost" , "user" ect. but i didn't get it. ion code is here,
Ion.with(this)
.load("http://marketplace.envato.com/api/edge/search:audiojungle,,happy.json")
.asJsonObject().setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception arg0, JsonObject data) {
// TODO Auto-generated method stub
}
});
所以,如果有人知道,请帮助我.预先谢谢你.
so if anybody knows then please help me. thank you in advance.
推荐答案
您发布的第一个信息是一些具有误导性的信息,我检查了您发布的答案并且工作正常代码给你,
The first info that you posted was has some misleading information, i checked the answer that you posted and i have a working code for you,
代码:
Ion.with(this)
.load("http://marketplace.envato.com/api/edge/search:audiojungle,,happy.json")
.asString().setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result) {
try {
parseJson(new JSONObject(result));
} catch (JSONException e1) {
e1.printStackTrace();
}
}
});
要解析响应数据:
private void parseJson(JSONObject result) {
try {
JSONArray jsonArray = result.getJSONArray("search");
for(int a=0;a<jsonArray.length();a++){
System.out.println(jsonArray.getJSONObject(a).getString("id"));
System.out.println(jsonArray.getJSONObject(a).getString("description"));
System.out.println(jsonArray.getJSONObject(a).getString("url"));
System.out.println(jsonArray.getJSONObject(a).getString("type"));
System.out.println(jsonArray.getJSONObject(a).getString("sales"));
System.out.println(jsonArray.getJSONObject(a).getString("rating"));
// JSON data with in JSONObject "item_info"
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("id"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("item"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("url"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("user"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("thumbnail"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("sales"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("rating"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("rating_decimal"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("cost"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("preview_type"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("preview_url"));
System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("length"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
- 请记住,使用JSONArray(org.json.JSONArray)和JSONObject(org.json.JSONObject)构成org.json库,而不是JsonArray(com.google.gson.JsonArray;)或JsonObject(com.google.gson). com.google.gson库中的JsonObject),它会使事情变得混乱;
- 为了使事情变得容易,请在此处粘贴响应 http://json.parser.online.fr/看看哪个是jsonobject或数组
- Remember to use JSONArray(org.json.JSONArray) and JSONObject(org.json.JSONObject) form the org.json libary not JsonArray(com.google.gson.JsonArray;) or JsonObject(com.google.gson.JsonObject) from the com.google.gson library, it get things mess up;
- To make things easy paste response here http://json.parser.online.fr/ to see which one is a jsonobject or array
这篇关于android解析嵌套的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文