简体   繁体   English

如何从JSONobject获取数据并将其显示在列表视图中?

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

This is my JSON 这是我的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) 现在,我尝试将其显示在listView中,这是我现在要执行的操作(出于测试目的,我只是尝试在烤面包中显示所有名称)

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? 如何使用多个名称?

您得到的是JSONObject的长度,但是您应该获得该JSONObject内的JSONArray的长度,以便遍历json数组项。

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

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'. 我建议使用“日志”而不是“ 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 谢谢

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" 仔细观察一下,您有一个Json,其中有一个数组,它是名称/键为“ data”的JsonArray

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. 制作一个模型类,该模型类可以在响应api或此json响应时存储值。

  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 现在,您可以添加for循环以保存值,也可以解析jason数组并将其保存到为处理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. 这是很容易被理解这个链接, 是一个工作示例解析JSON阵列并在列表视图中显示。 You have nothing to worry about after reading these two links. 阅读这两个链接后,您无需担心。

get your result from URL where your json is and store to any variable (result here) , then decode it i am showing below, 从您的json所在的URL获取结果并将其存储到任何变量(结果在此处),然后将其解码,如下所示,

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

                                }
                            }
                        }

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

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