Tash Nar
Tash Nar

Reputation: 471

How to get data from JSONobject and display it into a listview?

This is my JSON

{
    "data": [
        {
            "id": 1,
            "Name": "Choc Cake",
            "Image": "1.jpg",
            "Category": "Meal",
            "Method": "",
            "Ingredients": [
                {
                    "name": "1 Cup Ice"
                },
                {
                    "name": "1 Bag Beans"
                }
            ]
        },
        {
            "id": 2,
            "Name": "Ice Cake",
            "Image": "dfdsfdsfsdfdfdsf.jpg",
            "Category": "Meal",
            "Method": "",
            "Ingredients": [
                {
                    "name": "1 Cup Ice"
                }
            ]
        }
    ]
}

Now I am trying to display it into a listView how would I do that this is what i have right now (for testing purposes i am just trying to display all the names in a toast)

JSONObject jsonObj = new JSONObject(jsonStr);
int length = jsonObj .length(); 

for(int i=0; i<length; i++) {
    Toast.makeText(this, jsonObj.getJSONArray("data").
        getJSONObject(i).getString("Name"), Toast.LENGTH_LONG).show();
} 

The Above code only display one name and not multiple names. How can I make it for multiple names?

Upvotes: 1

Views: 16765

Answers (4)

user5320306
user5320306

Reputation:

Take a look this code snippet

//getting whole json string
JSONObject jsonObj = new JSONObject(jsonStr);

//extracting data array from json string
JSONArray ja_data = jsonObj.getJSONArray("data");
int length = jsonObj .length(); 

//loop to get all json objects from data json array
for(int i=0; i<length; i++) 
{
    JSONObject jObj = ja_data.getJSONObject(i);
    Toast.makeText(this, jObj.getString("Name").toString(), Toast.LENGTH_LONG).show();

    // getting inner array Ingredients
    JSONArray ja = jObj.getJSONArray("Ingredients");
    int len = ja.length();

    // getting json objects from Ingredients json array
    for(int j=0; j<len; j++)
    {
        JSONObject json = ja.getJSONObject(j);
        Toast.makeText(this, json.getString("name").toString(), Toast.LENGTH_LONG).show();
    }
} 

I recommend to use 'Log' instead using 'Toast'.

If any confusion or query let me know, i will try my best to resolve it. If answer is satisfiable please mark it as correct answer.

Happy coding! Thanks

Upvotes: 1

Amit Vaghela
Amit Vaghela

Reputation: 22945

get your result from URL where your json is and store to any variable (result here) , then decode it i am showing below,

try this , it may give you some hint , i have not tried but may help you

                        JSONObject jsonObject = new JSONObject(result);
                        JSONArray jsonArray = jsonObject.optJSONArray("data");

                        if (jsonArray != null) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObjects = jsonArray.optJSONObject(i);
                                int id = jsonObjects.optInt("id");
                                String varMessage = jsonObjects.optString("varMessage");
                                String Image = jsonObjects.optString("Image");
                                String Category = jsonObjects.optString("Category");
                                String Method = jsonObjects.optString("Method");

                               JSONArray jsonArrayIngredients = jsonObject.optJSONArray("Ingredients");

                               for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObjects = jsonArray.optJSONObject(i);

                                String name = jsonObjects.optString("name");

                                }
                            }
                        }

Upvotes: 0

Coas Mckey
Coas Mckey

Reputation: 709

I think you are guessing it wrong. Look closely you have a Json in that you have array which is JsonArray with the name/key "data"

then in that you can get it and traverse it index by index. For you I am providing you a road map so that things make easy for you conceptually

  1. Make a model class which may able to store the values as you are getting in response of your api or in this json respone.

  2. Take an array of type of your model class to store values

  3. Now you can add for loop to save values or you can parse and save your jason array into your array you made to handle the jasonarray

this is easily be understand by this link and this is a working example to parse the json array and to show in your list view. You have nothing to worry about after reading these two links.

Upvotes: 0

agamov
agamov

Reputation: 4427

You are getting the length of JSONObject, but you should get the length of JSONArray inside that JSONObject in order to iterate though json array items.

int length = jsonObj.getJSONArray("data").size()

Upvotes: 1

Related Questions