gpt4 book ai didi

android - 用于动态 json 键值的 Gson 解析器类 - Android

转载 作者:行者123 更新时间:2023-11-29 21:15:23 24 4
gpt4 key购买 nike

我使用 gson 库来解析 json 响应。它运作良好。现在我遇到了问题。

我从 webservice 得到以下响应。 json键值不是静态的,会动态变化。

如何编写解析器类来解析以下响应。

格式化的 JSON:

{
"meta": {
"code": 201,
"dataPropertyName": "activity",
"currentTime": "2014-02-05 06:15:04",
"listedCount": "2"
},
"activity": [
{
"comments": [
{
"raja": {
"type": "Liked",
"userPhoto": "663.png",
"userId": "74",
"userName": {
"1_0": "longjump"
},
"postOwner": "you",
"postDetails": {
"471": {
"postImage": "972.png",
"postId": "471",
"postType": "1"
}
},
"dateTime": "2014-02-05 05:24:56",
"sameOwner": "1"
}
}
]
},
{
"follow": [
{
"you": {
"type": "follow",
"followByUserName": {
"0_0": "olivepop",
"1_0": "yadidroy",
"2_0": "chitra"
},
"followUserName": "you",
"followByUserPhoto": "242.png",
"followUserPhoto": "953.png",
"dateTime": "2014-01-09 06:50:42"
}
}
]
}
],
"notifications": [
"Activities has been retrieved successfully"
]
}

最佳答案

使用这个解析器类

Meta meta = new Meta();

ArrayList<Activity> activity = new ArrayList<ActivityParser.Activity>();

ArrayList<String> notifications = new ArrayList<String>();

public class Meta
{
String code,dataPropertyName,currentTime,listedCount;
}

public class Activity
{
ArrayList<HashMap<String, CommentsItem>> comments = new ArrayList<HashMap<String,CommentsItem>>();

public class CommentsItem
{
String type,userPhoto,userId,postOwner,dateTime,sameOwner;
HashMap<String, String> userName = new HashMap<String,String>();
HashMap<String, PostDetails> postDetails = new HashMap<String,PostDetails>();
public class PostDetails
{
String postImage,postId,postType;
}

}

ArrayList<HashMap<String, FollowItem>> follow = new ArrayList<HashMap<String,FollowItem>>();

public class FollowItem
{
String type,followUserName,followByUserPhoto,followUserPhoto,dateTime;
HashMap<String, String> followByUserName = new HashMap<String,String>();
}
}

关于android - 用于动态 json 键值的 Gson 解析器类 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21570317/

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