当前位置: 代码迷 >> Android >> 请教关于json解析的对应有关问题
  详细解决方案

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

热度:23   发布时间:2016-04-28 01:30:26.0
请问关于json解析的对应问题
请问 以下是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"
                    }
                ]
            }
        }
    ]
}
------解决思路----------------------
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;
   ............
}




------解决思路----------------------

class Topic {
    public String topicId;
    public int total;
    public List<TopicMsg> list;
}

class TopicMsg {
   public List<ReplyMsg> msgList;
}

------解决思路----------------------
lz    推荐你可以去了解一下 谷歌提供的gson的一个jar包,解析json很方便,也很简单实用
------解决思路----------------------
关键是你的OOP 设计类怎么包装,比如你的帖子里面有多个回复 就是 一对多。
多个回复就是一个List 里面存储多个回复实例。
多个Topic
每个Topic包含多个Reply
  相关解决方案