java 如何从 JSONobject 获取数据并将其显示到列表视图中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32496565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get data from JSONobject and display it into a listview?
提问by Tash Nar
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?
上面的代码只显示一个名称而不是多个名称。我怎样才能为多个名字制作它?
采纳答案by agamov
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'.
我建议使用“Log”而不是“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
快乐编码!谢谢
回答by agamov
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.
你得到了 的长度JSONObject
,但你应该得到JSONArray
里面的长度,JSONObject
以便遍历 json 数组项。
int length = jsonObj.getJSONArray("data").size()
回答by Coas Mckey
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,因为你有一个数组,它是带有名称/键“数据”的 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
然后你可以得到它并按索引遍历它。对于您,我为您提供了一个路线图,以便在概念上让事情变得容易
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.
Take an array of type of your model class to store values
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
制作一个模型类,它可以在您响应 api 或在此 json 响应中存储值。
使用模型类的类型数组来存储值
现在您可以添加 for 循环来保存值,或者您可以解析您的 jason 数组并将其保存到您为处理 jasonarray 而制作的数组中
this is easily be understand by thislink and thisis 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.
回答by Amit Vaghela
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");
}
}
}