gpt4 book ai didi

java - 从 Json 数组中的 Json 数组获取数据 Android Java

转载 作者:太空狗 更新时间:2023-10-29 16:36:36 25 4
gpt4 key购买 nike

你好如何从另一个 Json 数组中的 Json 数组获取数据我已经获取了数据直到附件但附件不起作用,所有代码都有效直到附件如何从附件中获取数据我需要从中获取“photo_75”

JSON

"response":{  
"count":3,
"items":[
{
"id":3,
"from_id":205110032,
"owner_id":-81865402,
"date":1417672154,
"post_type":"post",
"text":"jjjjASDFGHJKYTRDXCVB",
"attachments":[
{
"type":"photo",
"photo":{
"id":330414711,
"album_id":-7,
"owner_id":205110032,
"photo_75":"http:\/\/cs605116.vk.me\/v605116032\/6325\/3SwTo8j4lJ0.jpg",
"photo_130":"http:\/\/cs605116.vk.me\/v605116032\/6326\/_OZA86FO3Nw.jpg",
"photo_604":"http:\/\/cs605116.vk.me\/v605116032\/6327\/AUtB59708Nw.jpg",
"photo_807":"http:\/\/cs605116.vk.me\/v605116032\/6328\/59oAdfz9jgI.jpg",
"width":538,
"height":807,
"text":"",
"date":1399134687,
"access_key":"7297eb663de2e4e6b2"
}
}
],
"comments":{
"count":0
},
"likes":{
"count":0
},
"reposts":{
"count":0
}
},

Java

private void parseJsonFeed(JSONObject response) {
try {
JSONObject parent = response.getJSONObject("response");

JSONArray feedArray = parent.getJSONArray("items");

for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);

FeedItem item = new FeedItem();
item.setId(feedObj.getInt("id"));


item.setName(feedObj.getString("post_type"));
item.setTimeStamp(feedObj.getString("date"));


// Image might be null sometimes
String image = feedObj.isNull("photo") ? null : feedObj
.getString("photo");
item.setImge(image);
item.setStatus(feedObj.getString("text"));

All code work till there how to get data from attachments
***JSONObject response1 = response.getJSONObject("response");
feedArray = parent.getJSONArray("items");***

JSONArray feedArray1 = response1.getJSONArray("attachments");

for (int i1 = 0; i1 < feedArray1.length(); i1++) {
JSONObject feedObj1 = (JSONObject) feedArray1.get(i1);

FeedItem item1 = new FeedItem();

item.setProfilePic(feedObj1.getString("photo_75"));


}



// url might be null sometimes
String feedUrl = feedObj.isNull("url") ? null : feedObj
.getString("url");
item.setUrl(feedUrl);

feedItems.add(item);
}

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

提前致谢

最佳答案

您在错误的对象中查找附件。 “附件”是项目的属性(property)。而不是

JSONArray feedArray1 = response1.getJSONArray("attachments");

使用

JSONArray feedArray1 = feedObj.getJSONArray("attachments");

在您的例子中,feedObj 包含项目对象。

获取照片:删除行:

        String image = feedObj.isNull("photo") ? null : feedObj
.getString("photo");
item.setImge(image);

并将其更改为:

    for (int i1 = 0; i1 < feedArray1.length(); i1++) {
JSONObject attachment = (JSONObject) feedArray1.get(i1);
JSONObject photo = (JSONObject) attachment.getJSONObject("photo");
item.setImge(photo);
item.setProfilePic(photo.getString("photo_75"));
item.setStatus(photo.getString("text"));
}

关于java - 从 Json 数组中的 Json 数组获取数据 Android Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27299226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com