[英]Accessing array inside an array
I'm trying to show images
array in a list but I didn't figured it out yet, can anyone please help me.我正在尝试在列表中显示images
数组,但我还没有弄清楚,谁能帮助我。 This may seem as a repeated question, but I tried other answers, they didn't work.这似乎是一个重复的问题,但我尝试了其他答案,但没有奏效。
My JSON:我的 JSON:
[
{
"id": 52,
"name": "viber",
"images": [
"7829-hit-the-vibe.gif",
"8413_Lit.gif",
"8095_Lazergunzoncam.gif",
"7090_LaserCamz.gif",
"2123-vibe-4.gif",
"3175-vibe-3.gif",
"7413-vibe-2.gif",
"1100-vibe.gif",
"6381-lazeroncamz-2.gif"
],
"download": null,
"amount": 9
},
{
"id": 45,
"name": "aesthetic",
"description": "a",
"slug": "6039-aesthetic",
"images": [
"4071_planet.png",
"3499_love_it.png",
"3019_space_bottle.png",
"6033_pixel_flower.png",
"1620-cupcake-pink.gif",
"2760-seashell-pink.gif",
"1794_sparkles.gif",
"2523_RamSip.png"
],
"download": null,
"amount": 8
},
]
My code:我的代码:
String jsonString = "myjson";
json1 = new Gson().fromJson(jsonString, new TypeToken<ArrayList<HashMap<String, Object>>>(){}.getType());
for(int _repeat24 = 0; _repeat24 < (int)(json1.size()); _repeat24++) {
JSONObject obj = new JSONObject(jsonString);
JSONArray getArray = obj.getJSONArray("images");
JSONObject objects = getArray.getJSONObject(_repeat24);
Iterator key = objects.keys();
while (key.hasNext()) {
String value = key.next();
}
}
My task: I'm trying to save the array images
as json in a key value, example:我的任务:我正在尝试将数组images
保存为键值中的 json,例如:
categoriesMap = new HashMap<>();
categoriesMap.put("name", name);
categoriesMap.put("imagesJson", JSON_I_NEED);
json1.add(categoriesMap);
This should work as a loop for all the array positions.这应该作为所有数组位置的循环。 Thanks.谢谢。
Use this way to parse your response:使用这种方式来解析您的响应:
ArrayList<Data> dataList = new ArrayList<Data>();
if(response!=null){
JSONArray mainArray = new JSONArray(response);
for(int i =0; i< mainArray.length();i++){
JSONObject itemObject = mainArray.optJSONObject(i);
Data data = new Data();
data.setName(itemObject.optString("name"));
ArrayList<String> images = new ArrayList<String>();
// parsing images array
String images = itemObject.optJSONArray("images").toString();
// this will set images array for each position : ["a.jpg","b.jpg","c.jpg"]
data.setImages(images);
dataList.add(data);
}
}
Now populate your list adapter with this ArrayList you will be able to access images for each position distinctly现在用这个 ArrayList 填充您的列表适配器,您将能够清楚地访问每个 position 的图像
To store images for each position:要存储每个 position 的图像:
Create a Data Class like this:像这样创建一个数据 Class:
class Data {
int id ;
String name;
String images;
int amount;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImages(){
return images;
}
public void setImages(String images){
this.images = images;
}
}
Now to access the images in recycler view adapter:现在访问回收器视图适配器中的图像:
dataList.get(position).getImages(); dataList.get(位置).getImages(); // ["a.jpg","b.jpg"] // ["a.jpg","b.jpg"]
String response = "YOUR JSON ARRAY FROM API RESPONSE";字符串响应 =“来自 API 响应的您的 JSON 数组”;
try {
JSONArray jsonArray = new JSONArray(response);
List<List<String>> imagesList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
JSONArray imagesArray = obj.getJSONArray("images");
List<String> images = new ArrayList<>();
for (int j = 0; j < imagesArray.length(); j++) {
images.add(imagesArray.getString(j));
}
imagesList.add(images);
}
// Now you have got a list of a list of images
Log.e(TAG, "onCreate: " + imagesList.toString());
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, "onCreate: ", e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.