繁体   English   中英

android解析嵌套的json对象

[英]android-parsing nested json Objects

我必须在我的应用程序中实现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数据。 我想在我的应用程序中显示“成本”,“用户”等数据。 但是我不明白。 离子代码在这里,

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

                }
            });

因此,如果有人知道,请帮助我。 先感谢您。

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

码:

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.JsonObject) com.google.gson库,它使事情变得一团糟;
  • 为了使事情变得容易,请在此处粘贴响应http://json.parser.online.fr/以查看哪个是jsonobject或数组

工艺流程:

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

编辑

首先:在使用索引获取其对象之后,您必须找到“搜索”数组,因此现在有了JsonObject,可以从中获取“ item_info”对象,从那里可以找到“ id”对象。

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

检查您的日志。

暂无
暂无

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

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