我试图从 flickr api 循环遍历 java 中的照片对象数组,但似乎无法定位照片数组,因为它嵌套在具有页面值的 json 对象中,如下所示。
{
"page" : 1,
"pages" : 10,
"perpage" : 100,
"total" : 1000,
"photo" : [
{photo objects}
]
}
我希望获得照片对象,然后将其传递给照片模型类。
最佳答案
我不知道您使用的是什么库,我个人使用 GSON,并将基于该库来编写示例。
这就是我的做法:
if(json.has("photo"){
JsonArray photoArray = json.get("photo").getAsJsonArray(); //get the full array
for(int i = 0; i < photoArray.size(); i++){ //loop through all photo objects inside the array
PhotoObject photoObject = photoArray.get(i);
//do something with your objects
}
}
关于java - 获取嵌套在另一个 json 对象中的 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56768932/