如何使用json和volley检索多维数组

时间:2022-10-22 20:51:16

I want to retreive data from json URL and i am face to a problem. I have this from the url :

我想从json URL中检索数据,我面临一个问题。我从网址得到这个:

 {
     -infos: (2)[
        -{
           value: "anderson",
           -pictures: (2)[
                 -{
                    picone: "text_pic",
                    url: http://www.example.com
                  },
                 -{
                    picone: "text_pic2",
                    url: http://www.example.com
                  }
            ]
        },
    -{
         value: "bryan",
           -pictures: (2)[
                 -{
                    picone: "text_pic3",
                    url: http://www.example.com
                  },
                 -{
                    picone: "text_pic4",
                    url: http://www.example.com
                  }
            ]
        },....

I have also my listview row :

我还有我的listview行:

 <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />

 <TextView
        android:id="@+id/url"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />


        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/pic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"/>

And here the volley code i used te retrieve data:

这里我使用的排球代码检索数据:

         ....
        private List<News_Item> nNews = new ArrayList<>();
    ......
            CustomRequest lnews = new CustomRequest(Request.Method.POST, URL, param, new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {

                                try {
                                    JSONArray vals= response.getJSONArray("infos");
                                    for (int i = 0; i < vals.length(); i++) {

                                        JSONObject dats= vals.getJSONObject(i);
                                        News_Item item = new News_Item ();
                                  item.setmTitle(dats.getString("value"));

                                      JSONArray overinfo = dats.getJSONArray("pictures");
                                        for (int j = 0; j < overinfo.length(); j++) {
                                            JSONObject links = overinfo .getJSONObject(j);
                                   item.setmImage(links.getString("picone"));
                                            item.setmUrl(links.getString("url"));

                                        }

                                       nNews.add(item);

                                    }

                                } catch (JSONException ex) {
                                    ex.printStackTrace();
                                }
....

This code return only one rows (the last row) for pictures array. something like this

此代码仅返回图片数组的一行(最后一行)。这样的事情

row 1 => anderson / text_pic2 / http://www.example.com
row 2 = bryan / text_pic4 /  http://www.example.com 

So please help

所以请帮忙

UPDATE This News_Item.class

更新此News_Item.class

    class News_Item {
        String mTitle;

     public News_Item (){

        }

      List<Picture> pictureList;

       public News_Item (String mTitle, <Picture> pictureList){
            this.mTitle= mTitle;
           this.pictureList= pictureList;
        }

  public String getmTitle() {
        return mTitle;
    }

    public void setmTitle(String mTitle) {
        this.mTitle = mTitle;
    }

      class Picture {
         String url;
         String picone;

     public Picture (String url, String picone) {
                this.url= url;
                this.picone= picone;
            }

            public String getUrl() {
                return Url;
            }

            public void setUrl(String url) {
                this.url = url;
            }

            public String getPicone() {
                return picone;
            }

            public void setPicone(String picone) {
                this.picone = picone;
            }


      }
    }

How get pictures datas.

如何获取图片数据。

3 个解决方案

#1


0  

Like in News_Item you did

就像在News_Item中一样

News_Item item = new News_Item ();
item.setmTitle(dats.getString("value"));

And at last added to list: nNews.add(item);

最后添加到列表中:nNews.add(item);

you need to add picture items too, So inside this loop, you are adding in for loop with same object i.e. item so its overwriting those variables

你需要添加图片项,所以在这个循环中,你是用相同的对象,即项添加for循环,所以它覆盖那些变量

for (int j = 0; j < overinfo.length(); j++) {
 JSONObject links = overinfo.getJSONObject(j);
 item.setmImage(links.getString("picone"));
 item.setmUrl(links.getString("url"));
-- Here add in collection of links
}

UPDATE: Here this will guide what you will need to do (I wrote directly on * editor so might have error)

更新:这将指导你需要做什么(我直接在*编辑器上写,所以可能有错误)

News_Item item = new News_Item();
item.setmTitle(dats.getString("value"));
List <Picture> pictures = new ArrayList<>();
for (int j = 0; j < overinfo.length(); j++) {
    JSONObject links = overinfo.getJSONObject(j);
    Picture picture = new Picture(links.getString("picone"), links.getString("url"));
    pictures.add(picture);
}
item.setmPictureList(pictures); // Create this or use constructor itself
nNews.add(item);

#2


0  

Try to change News_Item to have a list of Picture class.

尝试将News_Item更改为具有Picture类的列表。

Your code has a problem. You are overwriting new image in every looping.

您的代码有问题。您在每个循环中覆盖新图像。

JSONArray overinfo = dats.getJSONArray("pictures");
for (int j = 0; j < overinfo.length(); j++) {
    JSONObject links = overinfo .getJSONObject(j);
    item.setmImage(links.getString("picone"));
    item.setmUrl(links.getString("url"));
}

By creating new Picture class, you can add Picture object for picone and url value from pictures data.

通过创建新的Picture类,您可以从图片数据中为picone和url值添加Picture对象。

class News_Item {
  List<Picture> pictureList;
  ...
  class Picture {
     String url;
     String picone;
     ...
  }
}

#3


0  

Google has developed Gson, a library which lets you convert a JSON Strings to a Objects in just one line: Gson on GitHub

谷歌已经开发了Gson,这个库允许你将JSON字符串转换为一行中的对象:GitHub上的Gson

Given that these are your classes:

鉴于这些是你的课程:

public class InfoResponse {
    List<Info> infos;

    public InfoResponse(List<Info> infos) {
        this.infos = infos;
    }
}

public class Info {
    String value;
    List<Picture> pictures;

    public Info(String value, List<Picture> pictures) {
        this.value = value;
        this.pictures = pictures;
    }
}

public class Picture {
  String picone;
    String url;

    public Picture(String picone, String url) {
        this.picone = picone;
        this.url = url;
    }
}

you just have to call

你只需要打电话

InfoResponse response = new Gson().fromJson(jsonString, InfoResponse.class);

This will do everything for you.

这将为您做好一切。

#1


0  

Like in News_Item you did

就像在News_Item中一样

News_Item item = new News_Item ();
item.setmTitle(dats.getString("value"));

And at last added to list: nNews.add(item);

最后添加到列表中:nNews.add(item);

you need to add picture items too, So inside this loop, you are adding in for loop with same object i.e. item so its overwriting those variables

你需要添加图片项,所以在这个循环中,你是用相同的对象,即项添加for循环,所以它覆盖那些变量

for (int j = 0; j < overinfo.length(); j++) {
 JSONObject links = overinfo.getJSONObject(j);
 item.setmImage(links.getString("picone"));
 item.setmUrl(links.getString("url"));
-- Here add in collection of links
}

UPDATE: Here this will guide what you will need to do (I wrote directly on * editor so might have error)

更新:这将指导你需要做什么(我直接在*编辑器上写,所以可能有错误)

News_Item item = new News_Item();
item.setmTitle(dats.getString("value"));
List <Picture> pictures = new ArrayList<>();
for (int j = 0; j < overinfo.length(); j++) {
    JSONObject links = overinfo.getJSONObject(j);
    Picture picture = new Picture(links.getString("picone"), links.getString("url"));
    pictures.add(picture);
}
item.setmPictureList(pictures); // Create this or use constructor itself
nNews.add(item);

#2


0  

Try to change News_Item to have a list of Picture class.

尝试将News_Item更改为具有Picture类的列表。

Your code has a problem. You are overwriting new image in every looping.

您的代码有问题。您在每个循环中覆盖新图像。

JSONArray overinfo = dats.getJSONArray("pictures");
for (int j = 0; j < overinfo.length(); j++) {
    JSONObject links = overinfo .getJSONObject(j);
    item.setmImage(links.getString("picone"));
    item.setmUrl(links.getString("url"));
}

By creating new Picture class, you can add Picture object for picone and url value from pictures data.

通过创建新的Picture类,您可以从图片数据中为picone和url值添加Picture对象。

class News_Item {
  List<Picture> pictureList;
  ...
  class Picture {
     String url;
     String picone;
     ...
  }
}

#3


0  

Google has developed Gson, a library which lets you convert a JSON Strings to a Objects in just one line: Gson on GitHub

谷歌已经开发了Gson,这个库允许你将JSON字符串转换为一行中的对象:GitHub上的Gson

Given that these are your classes:

鉴于这些是你的课程:

public class InfoResponse {
    List<Info> infos;

    public InfoResponse(List<Info> infos) {
        this.infos = infos;
    }
}

public class Info {
    String value;
    List<Picture> pictures;

    public Info(String value, List<Picture> pictures) {
        this.value = value;
        this.pictures = pictures;
    }
}

public class Picture {
  String picone;
    String url;

    public Picture(String picone, String url) {
        this.picone = picone;
        this.url = url;
    }
}

you just have to call

你只需要打电话

InfoResponse response = new Gson().fromJson(jsonString, InfoResponse.class);

This will do everything for you.

这将为您做好一切。