android-parsing nested json Objects

你。 提交于 2019-12-04 21:02:00

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

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 .

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!