Question

Here is my function for initializing Json Object

public void setJsonsObject() throws ClientProtocolException, IOException, JSONException {

        StringBuilder url = new StringBuilder(URL);
        HttpGet get = new HttpGet(url.toString());
        HttpResponse response = client.execute(get);

        int status = response.getStatusLine().getStatusCode();
        if(status == 200) {
            HttpEntity entity = response.getEntity();
            String data = EntityUtils.toString(entity);
            jObj = new JSONObject(data);
            //JSONObject first = jsons.getJSONObject(1);
        } else {
            Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT);
        }       
    }

After this My jObj is

{
"object": [{
    "earnings": "0",
    "sizes": "",
    "original_price": "100",
    "add_date": "1376597956",
    "photo_two": "http:\/\/yoors.com\/api\/files\/520d3480dfafbyoors.jpg",
    "id": "1",
    "photo_three": "",
    "category": "Accessories",
    "title": "Minions",
    "listing_price": "80",
    "description": "Cute minions for sale",
    "users_user_id": "3",
    "update_date": "1376597956",
    "condition_p": "Used ",
    "photo_one": "http:\/\/yoors.com\/api\/files\/520d37c409992yoors.jpg"
}, {
    "earnings": "0",
    "sizes": "4",
    "original_price": "20",
    "add_date": "1376681417",
    "photo_two": "",
    "id": "2",
    "photo_three": "",
    "category": "Jeans",
    "title": "cute shorts",
    "listing_price": "20",
    "description": "shorts you'll love them",
    "users_user_id": "4",
    "update_date": "1376681417",
    "condition_p": "Brand",
    "photo_one": "http:\/\/yoors.com\/api\/files\/520e7dc94728cyoors.jpg"
}, {
    "earnings": "0",
    "sizes": "xl",
    "original_price": "25",
    "add_date": "1376683240",
    "photo_two": "http:\/\/yoors.com\/api\/files\/520e84e8aff0fyoors.jpg",
    "id": "3",
    "photo_three": "http:\/\/yoors.com\/api\/files\/520e84e8b06dfyoors.jpg",
    "category": "Accessories",
    "title": "very cute handbag",
    "listing_price": "25",
    "description": "nice clothes",
    "users_user_id": "4",
    "update_date": "1376683240",
    "condition_p": "Brand",
    "photo_one": "http:\/\/yoors.com\/api\/files\/520e84e8af73dyoors.jpg"
}, {
    "earnings": "0",
    "sizes": "size fits all",
    "original_price": "30",
    "add_date": "1377025807",
    "photo_two": "",
    "id": "4",
    "photo_three": "",
    "category": "Accessories",
    "title": "bling girl watch",
    "listing_price": "30",
    "description": "this watch is made in los angeles",
    "users_user_id": "4",
    "update_date": "1377025807",
    "condition_p": "Brand",
    "photo_one": "http:\/\/yoors.com\/api\/files\/5213bf0f403d8yoors.jpg"
}, {
    "earnings": "0",
    "sizes": "size 2-7",
    "original_price": "20",
    "add_date": "1377026129",
    "photo_two": "",
    "id": "5",
    "photo_three": "",
    "category": "Jeans",
    "title": "cute jean shorts",
    "listing_price": "20",
    "description": "shorts with pockets hanging out",
    "users_user_id": "4",
    "update_date": "1377026129",
    "condition_p": "Brand",
    "photo_one": "http:\/\/yoors.com\/api\/files\/5213c051f03d3yoors.jpg"
}, {
    "earnings": "0",
    "sizes": "all sizes",
    "original_price": "30",
    "add_date": "1377026853",
    "photo_two": "",
    "id": "6",
    "photo_three": "",
    "category": "Dress",
    "title": "very cute outfit",
    "listing_price": "30",
    "description": "this outfit is one of a kind",
    "users_user_id": "4",
    "update_date": "1377026853",
    "condition_p": "Brand",
    "photo_one": "http:\/\/yoors.com\/api\/files\/5213c325108efyoors.jpg"
}]}

After that I'm trying to get some information from jsonObject

setJsonsObject();
String str = jObj.getString("title");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage(str);
alertDialog.show();

And it gives JSONExeption: No value for title

What's the problem? Why it's not working well?

Was it helpful?

Solution 3

You are getting this Exception because "title" key is not directly inside your jObj .

parse like the following,

String data = EntityUtils.toString(entity);
                JSONObject jObj=new JSONObject(data);

                JSONArray jarray=jObj.getJSONArray("object");
                for(int i=0;i<jarray.length();i++)
                {
                    JSONObject obj=jarray.getJSONObject(i);
                    String title=obj.getString("title");
                }

title contains your required string .

OTHER TIPS

Please try this code for json parsing

 JsonArray object= json.getJSONArray("object");

    // looping through All Object
    for(int i = 0; i < object.length(); i++){
        JSONObject c = object.getJSONObject(i);

        // Storing each json item in variable
        String earning = c.getString("earnings");


    }

You need to get your JSONArray Object first:

JSONArray array = jObj.getJSONArray("object");
for (int i = 0; i < array.length(); i++) {
    JSONObject json = array.getJSONObject(i);
    String str = jObj.getString("title");
    //Doubt you want to show alert for each one. 
}

Alternatively, you can use droidQuery($) to simplify parsing:

Object[] array = $.makeArray(jObj.getJSONArray("object"));
for (Object obj : array) {
    JSONObject json = (JSONObject) obj;
    Map<String, ?> map = $.map(json);
    String str = map.get("title");
    //maybe show toast instead:
    $.with(this).toast(title, Toast.LENGTH_SHORT);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top