Json解析要点
解析Json时,昨天遇到了新的问题,之前都是解析的数组,不是数组的用类来做。
这是Json串;
{"status":"00001","ver":"1.0","msg":"ok","result":{"init_id":"13","init_time":"2015-12-30 00:00:00","init_title":"\u7bee\u7403\u7ea6\u67b6","init_lati":"31.19","init_longti":"120.37","init_location":"\u82cf\u5dde","commit_init_users":[{"commits_id":"25","commit_images":{"pub_img":["Public\/dd\/201512\/22\/5678ad286dfb8.jpg"],"pub_th_img":["Public\/dd\/thumb\/antu_5678ad286dfb8.jpg"]},"user_id":"1234648","user_head_url":"Public\/tmp\/7004.jpg","commits_time":"2015-12-30 00:00:00","commits_comments":" \u77ed\u53d1\n","commits_flg":"0","count":1},{"commits_id":"26","commit_images":{"pub_img":["Public\/dd\/201512\/22\/5678ad30337d8.jpg"],"pub_th_img":["Public\/dd\/thumb\/antu_5678ad30337d8.jpg"]},"user_id":"1234649","user_head_url":"Public\/tmp\/7005.jpg","commits_time":"2015-12-20 00:00:00","commits_comments":"\u5404\u4f4d\n","commits_flg":"0","count":1},{"commits_id":"40","commit_images":{"pub_img":["Public\/dd\/201512\/22\/5678ac6ec7a53.jpg","Public\/dd\/201512\/22\/5678ac7535f56.jpg"],"pub_th_img":["Public\/dd\/thumb\/antu_5678ac6ec7a53.jpg","Public\/dd\/thumb\/antu_5678ac7535f56.jpg"]},"user_id":"1234648","user_head_url":"Public\/tmp\/7004.jpg","commits_time":"2015-12-30 00:00:00","commits_comments":"\u7bee\u7403\u7ea6\u67b6","commits_flg":"1","count":2}],"all_count":4}}
这是解析这条Json的JavaBean对象:
/**JavBean对象的父类 * Created by cyc on 16-1-4. */ public class Response_Header_Info { @Expose public String status;//状态 @Expose public String ver;//版本 @Expose public String msg;//是否OK public String getStatus() { return status; } public String getVer() { return ver; } }
/**
* Square界面的Json对象 * Created by cyc on 16-1-4. */ public class Response_Square_Callup_Recommend_Info extends Response_Header_Info { private static final String TAG = "Response_Square_Callup_Recommend_Info"; /**一条广场活动的信息总和*/ @Expose public Squre_Activity_all result; /** * 一条广场活动信息的总和 */ public class Squre_Activity_all { /**所有参与这个活动的用户集合*/ @Expose public ArrayList<Init_users> commit_init_users; /**活动的图片总数*/ @Expose public int image_count; /**发起事件ID*/ @Expose public int init_id; /**事件的经度*/ @Expose public float init_lati; /**事件的地点*/ @Expose public String init_location; /**事件的纬度*/ @Expose public float init_longti; /**发起事件的时间*/ @Expose public String init_time; /**发起事件的标题*/ @Expose public String init_title; /**所有参与人和发起人发起图片的总和*/ @Expose public int all_count; } /** * 参与这个活动的用户类 */ public class Init_users { /**参与人的评论*/ @Expose public String commits_comments;//字段仔细别写错 /**是否活动发起人*/ @Expose public int commits_flg; /**参与发起的ID*/ @Expose public int commits_id; /**参与发起的时间*/ @Expose public String commits_time; /**参与事件人的头像*/ @Expose public String user_head_url; /**用户的ID*/ @Expose public int user_id; /**发布图片的数量*/ @Expose public int count; /**参与人发的所有图片的类*/ @Expose public Img commit_images; } /**参与人发的图片(缩略图和大图)*/ public class Img{ /**大图集合*/ @Expose public ArrayList<String> pub_img; /**缩略图集合*/ @Expose public ArrayList<String> pub_th_img; }
}
根据这个Json串的解析,绝大多数的Json都是这样解析的,一定要小心字段别写错了,要点: { 开头的字段用类来描述 [ 开头的字段用集合来描述