请问关于json解析的对应问题

Android 码拜 9年前 (2015-04-24) 896次浏览 0个评论
 

请问 以下是json的解析的, 这些数据是有关 发布帖子和在相应的帖子的 回复的数据(具体是 红色部分为 “chatid”: “ded562fb346815f054f6dc5de03dfd35″的帖子的 回复“reply”)
究竟怎么做到当点击某个帖子的时候,可以显示该帖子以及它的回复。
以下是我写的一部分json数据的解析,但是不知道怎么把reply跟帖子关联起来,麻烦会的朋友帮忙回复,谢谢
 try {
                JSONObject jsonObject = new JSONObject(info);
         String total = jsonObject.getString(“total”);
         JSONArray jsonArray = jsonObject.getJSONArray(“data”); 
         for( int i=0;i<jsonArray.length();i++ ) {
         JSONObject item = jsonArray.getJSONObject(i);
         String id = item.getString(“chatid”);
         String user = item.getString(“userid”);
         String photo = item.getString(“photo”);
         String nick = item.getString(“nickname”);
         String full = item.getString(“fullname”);
         String s = item.getString(“sex”);
         String f1 = item.getString(“from1”);
         String f2 = item.getString(“from2”);
         String li  = item.getString(“like”);
         String ty = item.getString(“type”);
         String cont = item.getString(“content”);
         JSONArray jsonArrayPic = item.getJSONArray(“pic”); 
         String []b = new String[jsonArrayPic.length()];
         for( int j=0;j<jsonArrayPic.length();j++ ) {
         b[j] = jsonArrayPic.getString(j);
         }
         String time = item.getString(“time”);
         JSONObject jsonObjectReply = new JSONObject(item.getString(“reply“));

从网络数据库中获取了以下json数据:
info={“total”:3,”data”:[{“chatid”:”e3eb8de39464dd15584abbbd4dad0e14″,”userid”:”6633e3d4aeb0dcbc8c4b7224160b7095″,”photo”:”http:\/\/abc.com\/img.php\/e809a9b957951f11979116363854a1c0\/w_80\/h_80″,”nickname”:”小强”,”fullname”:”咨询”,”sex”:”1″,”from1″:”销售”,”from2″:”医药公司”,”like”:”0″,”type”:”pic”,”content”:”在线求解梦”,”pic”:[“http:\/\/abc.com\/img.php\/e22b47920dd31447c9881dfc1bc5ef67″,”http:\/\/abc.com\/img.php\/9272da50ad90bcd4fbe470953e485abe”],”time”:”2015-04-07 23:13:30″,”reply”:{“total”:0,”data”:[]}},{“chatid”:”ba111641cef87c00850a3c5d03f63569″,”userid”:”6633e3d4aeb0dcbc8c4b7224160b7095″,”photo”:”http:\/\/abc.com\/img.php\/e809a9b957951f11979116363854a1c0\/w_80\/h_80″,”nickname”:”小强”,”fullname”:”咨询”,”sex”:”1″,”from1″:”销售”,”from2″:”医药公司”,”like”:”1″,”type”:”pic”,”content”:”r?t?y?y?y?yu”,”pic”:[“http:\/\/abc.com\/img.php\/338b24cf3eee36c52ff9b8e1bfeb9cde”],”time”:”2015-04-07 23:12:33″,”reply”:{“total”:0,”data”:[]}},{“chatid”:”ded562fb346815f054f6dc5de03dfd35″,”userid”:”6633e3d4aeb0dcbc8c4b7224160b7095″,”photo”:”http:\/\/abc.com\/img.php\/e809a9b957951f11979116363854a1c0\/w_80\/h_80″,”nickname”:”小强”,”fullname”:”咨询”,”sex”:”1″,”from1″:”销售”,”from2″:”医药公司”,”like”:”5″,”type”:”pic”,”content”:”招聘经理”,”pic”:[“http:\/\/abc.com\/img.php\/39b408ed42f55d26411f71a4740ac7bd”,”http:\/\/abc.com\/img.php\/cebc31a18548995bb4ec522f0b3d2049″,”http:\/\/abc.com\/img.php\/d966fd0d574ae96fbe96003fbf5b19fc”],”time”:”2015-03-20 00:11:48″,”reply”:{“total”:1,”data”:[{“chatid”:”09690b2b77566ce645b834b3b038cc68″,”userid”:”8012be515cc9244ef263e91d2b442349″,”photo”:””,”nickname”:”551200o”,”fullname”:””,”sex”:”1″,”from1″:”销售”,”from2″:”医药公司”,”content”:”ok”,”time”:”2015-04-08 18:14:02″}]}}]}

用json 工具可以把以上数据转化为以下表示方式
info={
    “total”: 3,
    “data”: [
        {
            “chatid”: “e3eb8de39464dd15584abbbd4dad0e14”,
            “userid”: “6633e3d4aeb0dcbc8c4b7224160b7095”,
            “photo”: “http:\/\/abc.com\/img.php\/e809a9b957951f11979116363854a1c0\/w_80\/h_80”,
            “nickname”: “小强”,
            “fullname”: “咨询”,
            “sex”: “1”,
            “from1”: “销售”,
            “from2”: “医药公司”,
            “like”: “0”,
            “type”: “pic”,
            “content”: “在线求解梦”,
            “pic”: [
                “http:\/\/abc.com\/img.php\/e22b47920dd31447c9881dfc1bc5ef67”,
                “http:\/\/abc.com\/img.php\/9272da50ad90bcd4fbe470953e485abe”
            ],
            “time”: “2015-04-07 23:13:30”,
            “reply”: {
                “total”: 0,
                “data”: [
                    
                ]
            }
        },
        {
            “chatid”: “ba111641cef87c00850a3c5d03f63569”,
            “userid”: “6633e3d4aeb0dcbc8c4b7224160b7095”,
            “photo”: “http:\/\/abc.com\/img.php\/e809a9b957951f11979116363854a1c0\/w_80\/h_80”,
            “nickname”: “小强”,
            “fullname”: “咨询”,
            “sex”: “1”,
            “from1”: “销售”,
            “from2”: “医药公司”,
            “like”: “1”,
            “type”: “pic”,
            “content”: “r?t?y?y?y?yu”,
            “pic”: [
                “http:\/\/abc.com\/img.php\/338b24cf3eee36c52ff9b8e1bfeb9cde”
            ],
            “time”: “2015-04-07 23:12:33”,
            “reply”: {
                “total”: 0,
                “data”: [
                    
                ]
            }
        },
        {
            “chatid”: “ded562fb346815f054f6dc5de03dfd35”,
            “userid”: “6633e3d4aeb0dcbc8c4b7224160b7095”,
            “photo”: “http:\/\/abc.com\/img.php\/e809a9b957951f11979116363854a1c0\/w_80\/h_80”,
            “nickname”: “小强”,
            “fullname”: “咨询”,
            “sex”: “1”,
            “from1”: “销售”,
            “from2”: “医药公司”,
            “like”: “5”,
            “type”: “pic”,
            “content”: “招聘经理”,
            “pic”: [
                “http:\/\/abc.com\/img.php\/39b408ed42f55d26411f71a4740ac7bd”,
                “http:\/\/abc.com\/img.php\/cebc31a18548995bb4ec522f0b3d2049”,
                “http:\/\/abc.com\/img.php\/d966fd0d574ae96fbe96003fbf5b19fc”
            ],
            “time”: “2015-03-20 00:11:48”,
            “reply”: {
                “total”: 1,
                “data”: [
                    {
                        “chatid”: “09690b2b77566ce645b834b3b038cc68”,
                        “userid”: “8012be515cc9244ef263e91d2b442349”,
                        “photo”: “”,
                        “nickname”: “551200o”,
                        “fullname”: “”,
                        “sex”: “1”,
                        “from1”: “销售”,
                        “from2”: “医药公司”,
                        “content”: “ok”,
                        “time”: “2015-04-08 18:14:02”
                    }
                ]
            }
        }
    ]
}

请问关于json解析的对应问题
15分
reply 是个JSON Object ,里面的 data 是JSONArray
你可以创建一个实体类去封装这个Reply

class Topic {
    public String topicId;
    List<Reply> replyList;
}
class Reply {
   public int total; 
   public String topicId; //这个是帖子ID
   public List<ReplyMsg> msgList;

}
class ReplyMsg {
   public String chatid;
   public String userid;
   public String nickname;
   public String time;
   ............
}



请问关于json解析的对应问题
15分
class Topic {
    public String topicId;
    public int total;
    public List<TopicMsg> list;
}

class TopicMsg {
   public List<ReplyMsg> msgList;
}
请问关于json解析的对应问题
引用 2 楼 birdsaction 的回复:
class Topic {
    public String topicId;
    public int total;
    public List<TopicMsg> list;
}

class TopicMsg {
   public List<ReplyMsg> msgList;
}

birdsaction   非常感谢您的回复
但是我有个地方不是很明白。 比如说  帖子是可以发表好多个的。也就是说  应该有个 帖子list。
那么这个帖子list 是不是也要用类封装起来呢?

具体我实在是不太清楚怎么写,如果方便的话,能不能再帮忙详细写下呢?  先谢啦

也就是说要实现这种效果
—-  刚开始手机上显示
帖子A
帖子B
帖子C
帖子D
。。。
——

当点击帖子A的时候,会显示
帖子A
回复a1
回复a2
回复a3
。。。
 ——-
当点击帖子C的时候,会显示
帖子C
回复c1
回复c2
回复c3
。。
也就是说每个帖子要跟它的回复对应起来。

请问关于json解析的对应问题
20分
lz    推荐你可以去了解一下 谷歌提供的gson的一个jar包,解析json很方便,也很简单实用
请问关于json解析的对应问题
引用 4 楼 u014081087 的回复:

lz    推荐你可以去了解一下 谷歌提供的gson的一个jar包,解析json很方便,也很简单实用

@u014081087  谢谢您的回复
不过我之前没有看过gson这方面的,项目紧,我觉得birdsaction 说的解决方法是挺好的,不过我不太会具体怎么写。
那个gson的 对应我上面的json的解析复杂吗? 如果可以的话,麻烦说下大概是怎么处理吧。

请问关于json解析的对应问题
50分
关键是你的OOP 设计类怎么包装,比如你的帖子里面有多个回复 就是 一对多。
多个回复就是一个List 里面存储多个回复实例。
多个Topic
每个Topic包含多个Reply
请问关于json解析的对应问题
引用 6 楼 birdsaction 的回复:

关键是你的OOP 设计类怎么包装,比如你的帖子里面有多个回复 就是 一对多。
多个回复就是一个List 里面存储多个回复实例。
多个Topic
每个Topic包含多个Reply

@ birdsaction    谢谢您的回复

那就是说 在比如有多个帖子的话, 就把这些帖子放在 list里边
比如 List<帖子类> ChatList = new ArrayList<帖子类>();
然后帖子类里边存储回复的实例。

请问 你是这个意思吗? 麻烦再回复下   谢谢


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明请问关于json解析的对应问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!