【问题标题】:How to fetch Json array data in android studio using volley?如何使用 volley 在 android studio 中获取 Json 数组数据?
【发布时间】:2019-03-19 07:10:04
【问题描述】:

我有一个 JSON 编码数组,如下所示:-

{
"imager": [{
    "title": "Guru",
    "images": ["images\/6.png", "images\/androidIntro.png", "images\/barretr_Earth.png"]
}]

}

我的问题是我想一张一张地从图像数组中获取所有图像,以便我可以在 imageview 上显示图像。我的主要目标是只显示一次标题并显示与标题相关的所有图像,我在整个互联网和堆栈流中进行了搜索,但我无法找到正确的答案 谁能帮我解决这个问题? 我正在使用 Volley Libabry,这是我的代码:-

  url = "myurl";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {


                JSONArray jsonArray = response.getJSONArray("imager");

                for (int i=0; i<jsonArray.length(); i++)
                {
                    JSONObject object = jsonArray.getJSONObject(i);
                    String title = object.getString("title");
                    String images = object.getString("images");

                   // what to do here now?? help me?
                }

如果我设置

textview.setText(images);
output will be like this:-
["images\/6.png","images\/androidIntro.png","images\/barretr_Earth.png"]

但我只想要 图片/6.png 图像/androidIntro.png 图片/battetr_Earth.png

这样我就可以在 imageview 中显示这些所有图像。

【问题讨论】:

    标签: android arrays json sorting


    【解决方案1】:

    你必须把"images"标签当作一个数组来处理。

    代替

    String images = object.getString("images");
    

    使用

    JsonArray images = object.getJSONArray("images");
    for (int j=0; j<images.length(); j++) {
        String image = images.getString(j)
        // image will be 
        // j = 0 -> "images\/6.png"
        // j = 1 -> "images\/androidIntro.png"
        // j = 2 -> "images\/barretr_Earth.png"
    }
    

    【讨论】:

    • 我已经完成了这部分,在你的帮助下,谢谢你,但现在我面临另一个问题,我也得到了正确的所有图像和标题,但问题是我得到了多个标题,比如这个 {image: images/6.png title: guru} {image: images/androidIntro.png title: guru} {image: images/barretr_Earth.png title: guru} 我只想标题显示一次
    • 简单的不显示第二个里面的标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2022-01-10
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多