【问题标题】:Adding JSON data to arrayList将 JSON 数据添加到 arrayList
【发布时间】:2011-10-13 01:57:36
【问题描述】:

仍在处理我的脚本的这一部分以解析我的 JSON jsonArray = new JSONArray(jsonData);,然后将其添加到 arraylist "myJSONArray"

我不确定 for 循环如何将我的元素从 jsonArray 添加到 myJSONArray (arrayList)

如果我不清楚,请告诉我,如果我缺少任何信息,请询问。提前致谢。

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 类:

public class image_data {


    public int id;
    public String name;
    public String thumb;
    public String path;


}

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

    }
}

【问题讨论】:

  • 显示 JSON 输出..或至少部分显示需要解析的对象。
  • 将 JSON 数据安排为另一种方式可能更有意义 - 对象数组,而不是数组对象。
  • @Karl JSON 数据来自我的 MySQL 查询 -> json_encode 这样。我会改变我的查询以以不同的顺序提取数据吗?

标签: java android json arraylist


【解决方案1】:

你如何迭代和添加

 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());
    }

只需添加正确的键名。我不认识他们。

【讨论】:

  • @Nikola 你能分享你在 for 循环中得到的响应吗?
【解决方案2】:

你应该发布 json 数据的样子。 假设 json 数据看起来像

...,{
    "image_data": {
        "name": "image name","thumb":"thumbpath","path":"path"}
},{
    "image_data": {
        "name": "image name 2","thumb":"thumbpath 2","path":"path 2"}
}

然后你会像这样提取值:

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

         }

考虑为您的 image_data 类使用 getter。 也可以考虑将其重命名为 ImageData

【讨论】:

  • 对不起,我编辑了我的帖子并添加了 json 布局。 +1 感谢您的帖子很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
  • 1970-01-01
  • 2012-02-15
相关资源
最近更新 更多