问题
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"
}
} ] }
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.
回答1:
EDIT: 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,
code:
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();
}
}
});
To parse the response data:
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();
}
}
- 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
回答2:
Process :
JsonArray searchArray = getJsonArray("search");
JsonObject index_0_Object= searchArray.getJsonObject("index_0");
JsonObject infoObject = index_0_Object.getJsonObject("item_info");
String id = infoObject.getString("id");
System.out.pritnln(id);
Edited :
first : you have to find "search" array , after it use indexing to get its object , so now you have JsonObject , from it get "item_info" object , from there you can find "id" object .
回答3:
> yes i got it...
Ion.with(this)
.load("http://marketplace.envato.com/api/edge/search:audiojungle,,happy.json")
.asString().setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception arg0, String data) {
// TODO Auto-generated method stub
try {
JSONObject jObject = new JSONObject(data);
JSONArray jArray = jObject.getJSONArray("search");
for ( int i = 0 ; i < jArray.size() ; i++ )
{
JSONObject jObject_0 = jArray.getJSONObject(i);
JSONObject jObj = jObject_0.getJSONObject("id");
Log.e("id : ",id+"");
}
//JSONObject jObject_0 = jArray.getJSONObject(1);
//JSONObject jObj = jObject_0.getJSONObject("item_info");
//cost.setText(jObj.getString("cost"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
> but this returns only one value. i want to multiple value according to array list.
check your logs .
来源:https://stackoverflow.com/questions/26653215/android-parsing-nested-json-objects