java 将 JSON 数据添加到 arrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6824833/
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
Adding JSON data to arrayList
提问by Denoteone
Still working on this part of my script to parse my JSON jsonArray = new JSONArray(jsonData);
and then adding it to a arraylist "myJSONArray"
仍在处理我脚本的这一部分以解析我的 JSON jsonArray = new JSONArray(jsonData);
,然后将其添加到数组列表“myJSONArray”中
I am not sure how the for loop would work to add my elements from jsonArray into myJSONArray (arrayList)
我不确定 for 循环如何将我的元素从 jsonArray 添加到 myJSONArray (arrayList)
If I am not clear please let me know and if there is any info I am missing just ask. Thanks in advance.
如果我不清楚,请告诉我,如果我遗漏了任何信息,请询问。提前致谢。
JSON data:
JSON 数据:
{"id":["1","2","3"],"name":["Dragon","Butterfly","Tattoo"],"thumb":["thm_polaroid.jpg","thm_default.jpg","thm_enhanced-buzz-9667-1270841394-4.jpg"],"path":["polaroid.jpg","default.jpg","enhanced-buzz-9667-1270841394-4.jpg"]}
image_data class:
图像数据类:
public class image_data {
public int id;
public String name;
public String thumb;
public String path;
}
ShowThumb class:
ShowThumb 类:
public class showThumb extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridlayout);
Bundle bundle = getIntent().getExtras();
String jsonData = bundle.getString("jsonData");
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(jsonData);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<image_data> myJSONArray = new ArrayList<image_data>();
for (int i=0; i<jsonArray.length(); i++) {
**//This is where I think I create the seperate arrays of id name thumb and path**
myJSONArray.add(new image_data());
}
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this,image_data));
}
}
回答by Nikola Despotoski
How you iterate and add
你如何迭代和添加
for(int i=0;i<jsonArray.length;i++)
{
JSONObject json_data = jArray.getJSONObject(i);
imgnfo.id = json_data.getInt("id_key");
imgnfo.name = json_data.getString("name_key");
imgnfo.thumb = json_data.getString("thumb_key");
imgnfo.info = json_data.getString("info_key");
myArray.add(new image_data());
}
Just add proper keys names. I didn't know them.
只需添加正确的键名。我不认识他们。
回答by Nelson Ramirez
Yo should post what the json data looks like. Assuming the json data looks something like
你应该发布 json 数据的样子。假设 json 数据看起来像
...,{
"image_data": {
"name": "image name","thumb":"thumbpath","path":"path"}
},{
"image_data": {
"name": "image name 2","thumb":"thumbpath 2","path":"path 2"}
}
Then you would extract the values like so:
然后你会像这样提取值:
ArrayList<image_data> imageArray = new ArrayList<image_data>();
for (int i=0; i<jsonArray.length(); i++) {
image_data img = new image_data();
Object jo = jsonArray.get(i);
image_data.name = jo.name;
image_data.thumb = jo.thumb;
image_data.path = jo.path;
imageArray.add(new image_data());
}
Consider using getters for you image_data class. Also consider renaming it to ImageData
考虑为您的 image_data 类使用 getter。还可以考虑将其重命名为 ImageData