如何使用volley在android studio中获取Json数组数据?

时间:2022-10-22 20:46:33

I have a JSON encode array given bellow:-

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

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

}

My Problem is that I want to fetch all the images from images array one by one so that I can show the images on imageview. my Main aim is to display Title only one time and show all the images related to the title, I have searched all over the internet and stackflow but I am unable to find the proper answer could anyone help me solving this? I am using Volley Libabry here is my code:-

我的问题是我想逐个从图像数组中获取所有图像,以便我可以在imageview上显示图像。我的主要目的是只显示标题一次,并显示所有与标题相关的图像,我已经搜索了整个互联网和stackflow但我无法找到正确的答案,任何人都可以帮我解决这个问题?我在使用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?
                }

if I set

如果我订

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

but i want only images/6.png images/androidIntro.png images/battetr_Earth.png

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

So that I can so these all images in imageview.

这样我就可以将所有这些图像都放在imageview中。

1 个解决方案

#1


0  

You have to handle the "images" tag as an array.

您必须将“images”标记作为数组处理。

Instead of

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

use

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"
}

#1


0  

You have to handle the "images" tag as an array.

您必须将“images”标记作为数组处理。

Instead of

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

use

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"
}