android-parsing嵌套的json对象

时间:2014-10-30 12:38:19

标签: android json nested ion

我必须在我的应用程序中实现json数据。但我无法从服务器获取数据。我正在使用ION库。

{
"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数据。我希望在我的应用程序数据中显示" cost" ,"用户"等。但我没有得到它。离子代码在这里,

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

                }
            });

所以如果有人知道那么请帮助我。提前谢谢你。

3 个答案:

答案 0 :(得分:2)

编辑:您发布的第一个信息有一些误导性信息,我检查了您发布的答案,我有适合您的工作代码,

代码:

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或数组

答案 1 :(得分:0)

流程:

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

已修改

首先:你必须找到&#34;搜索&#34;数组之后,它使用索引来获取它的对象,所以现在你有了JsonObject,从它得到&#34; item_info&#34;对象,从那里你可以找到&#34; id&#34;对象。

答案 2 :(得分:0)

> 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.

检查你的日志。