0

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.

5
  • from object data get array "search", then take one of its elements(or iterate the array), then the element "item_info", then take element "cost" or "user" ... and why it gets +1? this question is typical "how to parse json with X library" answered many times here Commented Oct 30, 2014 at 12:42
  • but i really didn't get any idea about that. please give me any reference. Commented Oct 30, 2014 at 12:43
  • your JSON data is invalid Fotomat SyntaxError: JSON.parse: expected ',' or ']' after array element at line 23 column 3 of the JSON data Commented Oct 30, 2014 at 12:44
  • ok: check which json library is used by ION, then find documentation to this library, read it(or google for samples), write the code ... simply enough? Commented Oct 30, 2014 at 12:45
  • Handling Json with the Gson library is easier IMHO. Commented Oct 31, 2014 at 17:13

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

@NikPastagia what do you mean with "get multiple values for single object"
i mean to say item_info object is repeated in every array index value... so i want to get every index value of array...
@NikPastagia form what i understand that you only want item_info data from each array than you have to use jsonArray.getJSONObject(a).getJSONObject("item_info") in the loop. you will get the compleate item_info object of each array.
@NikPastagia [email protected]
Hello does this mean that i can Set my setters in the for loop to bind to my Recyclerview ?, how can i bind that Json data to my RecyclerView?
0

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 Comments

also not working for me... your answer is for GSON.. every time i used it not getting any value... simple use JSONObject or JSONArray not use JsonArray..
buddy i have added this as a process , try to parse according to this answer .
here, no method like getJsonArray("search").
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.

check your logs .

3 Comments

use jArray.size() , and use forloop .
every time it gives me single value.
there is only one object present in your json .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.