Android json解析,史上最难

逆天先生 2015-11-18 09:15:35
我们后台给了我一个数据,让我解析,怎么解析呢,求助
{
"data": {
"uid": "213",
"user_name": "萌小八",
"user_image": null,
"user_address": "中国北京朝阳",
"id": "63",
"path1": "20151117/564aa3d635f74.png",
"path2": "20151117/564aa3d637268.png",
"path3": null,
"path4": null,
"path5": null,
"path6": null,
"path7": null,
"path8": null,
"path9": null,
"content": "太好吃了",
"type": "2",
"imgnum": "2",
"count": "1",
"c_time": "1447732182",
"special": null,
"cnum": "0",
"gz": 3,
"dz": 3,
"sc": 3,
"sid": null,
"labels": [
{
"lname": "粉丝",
"lid": "57"
},
{
"lname": "酸菜",
"lid": "58"
}
]
},
"info": [
[
{
"lid": "56",
"lname": "好好吃",
"xaxis": "0.5989583134651184",
"yaxis": "0.3119140565395355",
"direct": "left",
"lclass": "zl"
}
],
[
{
"lid": null,
"lname": null,
"xaxis": null,
"yaxis": null,
"direct": null,
"lclass": null
}
]
],
"status": 1
}
...全文
292 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
猴子写代码 2015-11-19
  • 打赏
  • 举报
回复
楼上已经给你实体类了,还有解析方法,用gson你的数据解析成bean对象的时候会遇到问题哦,你的字符串中有null,会解析出错,在初始化gson对象的时候要这样写
Gson gson = new GsonBuilder().serializeNulls().create();
蒲锦_up 2015-11-19
  • 打赏
  • 举报
回复
这个也算难/?
孤独小书生 2015-11-19
  • 打赏
  • 举报
回复
用GSON来解析,放在一个对象里面,里外曾一层层对象解析,这个问题很基础吧
jklwan 2015-11-19
  • 打赏
  • 举报
回复
public class DataGroup {
    
    private DataEntity data;
    
    private int status;
    
    private List<List<InfoEntity>> info;
    
    public void setData(DataEntity data) {
        this.data = data;
    }
    public void setStatus(int status) {
        this.status = status;
    }

    public void setInfo(List<List<InfoEntity>> info) {
        this.info = info;
    }

    public DataEntity getData() {
        return data;
    }

    public int getStatus() {
        return status;
    }

    public List<List<InfoEntity>> getInfo() {
        return info;
    }

    public static class DataEntity {
        private String uid;
        private String user_name;
        private Object user_image;
        private String user_address;
        private String id;
        private String path1;
        private String path2;
        private Object path3;
        private Object path4;
        private Object path5;
        private Object path6;
        private Object path7;
        private Object path8;
        private Object path9;
        private String content;
        private String type;
        private String imgnum;
        private String count;
        private String c_time;
        private Object special;
        private String cnum;
        private int gz;
        private int dz;
        private int sc;
        private Object sid;
        /**
         * lname : 粉丝
         * lid : 57
         */

        private List<LabelsEntity> labels;

        public void setUid(String uid) {
            this.uid = uid;
        }

        public void setUser_name(String user_name) {
            this.user_name = user_name;
        }

        public void setUser_image(Object user_image) {
            this.user_image = user_image;
        }

        public void setUser_address(String user_address) {
            this.user_address = user_address;
        }

        public void setId(String id) {
            this.id = id;
        }

        public void setPath1(String path1) {
            this.path1 = path1;
        }

        public void setPath2(String path2) {
            this.path2 = path2;
        }

        public void setPath3(Object path3) {
            this.path3 = path3;
        }

        public void setPath4(Object path4) {
            this.path4 = path4;
        }

        public void setPath5(Object path5) {
            this.path5 = path5;
        }

        public void setPath6(Object path6) {
            this.path6 = path6;
        }

        public void setPath7(Object path7) {
            this.path7 = path7;
        }

        public void setPath8(Object path8) {
            this.path8 = path8;
        }

        public void setPath9(Object path9) {
            this.path9 = path9;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public void setType(String type) {
            this.type = type;
        }

        public void setImgnum(String imgnum) {
            this.imgnum = imgnum;
        }

        public void setCount(String count) {
            this.count = count;
        }

        public void setC_time(String c_time) {
            this.c_time = c_time;
        }

        public void setSpecial(Object special) {
            this.special = special;
        }

        public void setCnum(String cnum) {
            this.cnum = cnum;
        }

        public void setGz(int gz) {
            this.gz = gz;
        }

        public void setDz(int dz) {
            this.dz = dz;
        }

        public void setSc(int sc) {
            this.sc = sc;
        }

        public void setSid(Object sid) {
            this.sid = sid;
        }

        public void setLabels(List<LabelsEntity> labels) {
            this.labels = labels;
        }

        public String getUid() {
            return uid;
        }

        public String getUser_name() {
            return user_name;
        }

        public Object getUser_image() {
            return user_image;
        }

        public String getUser_address() {
            return user_address;
        }

        public String getId() {
            return id;
        }

        public String getPath1() {
            return path1;
        }

        public String getPath2() {
            return path2;
        }

        public Object getPath3() {
            return path3;
        }

        public Object getPath4() {
            return path4;
        }

        public Object getPath5() {
            return path5;
        }

        public Object getPath6() {
            return path6;
        }

        public Object getPath7() {
            return path7;
        }

        public Object getPath8() {
            return path8;
        }

        public Object getPath9() {
            return path9;
        }

        public String getContent() {
            return content;
        }

        public String getType() {
            return type;
        }

        public String getImgnum() {
            return imgnum;
        }

        public String getCount() {
            return count;
        }

        public String getC_time() {
            return c_time;
        }

        public Object getSpecial() {
            return special;
        }

        public String getCnum() {
            return cnum;
        }

        public int getGz() {
            return gz;
        }

        public int getDz() {
            return dz;
        }

        public int getSc() {
            return sc;
        }

        public Object getSid() {
            return sid;
        }

        public List<LabelsEntity> getLabels() {
            return labels;
        }

        public static class LabelsEntity {
            private String lname;
            private String lid;

            public void setLname(String lname) {
                this.lname = lname;
            }

            public void setLid(String lid) {
                this.lid = lid;
            }

            public String getLname() {
                return lname;
            }

            public String getLid() {
                return lid;
            }
        }
    }

    public static class InfoEntity {
        private String lid;
        private String lname;
        private String xaxis;
        private String yaxis;
        private String direct;
        private String lclass;

        public void setLid(String lid) {
            this.lid = lid;
        }

        public void setLname(String lname) {
            this.lname = lname;
        }

        public void setXaxis(String xaxis) {
            this.xaxis = xaxis;
        }

        public void setYaxis(String yaxis) {
            this.yaxis = yaxis;
        }

        public void setDirect(String direct) {
            this.direct = direct;
        }

        public void setLclass(String lclass) {
            this.lclass = lclass;
        }

        public String getLid() {
            return lid;
        }

        public String getLname() {
            return lname;
        }

        public String getXaxis() {
            return xaxis;
        }

        public String getYaxis() {
            return yaxis;
        }

        public String getDirect() {
            return direct;
        }

        public String getLclass() {
            return lclass;
        }
    }
}
解析的时候用Gson解析,使用DataGroup dataGroup = new Gson().fromJson("json字符串",DataGroup.class);
sanxiaochengyu 2015-11-19
  • 打赏
  • 举报
回复
private void ParseJson() {
String jsonString = "{\n" + "    \"data\": {\n"
				+ "        \"uid\": \"213\",\n"
				+ "        \"user_name\": \"萌小八\uE110\",\n"
				+ "        \"user_image\": null,\n"
				+ "        \"user_address\": \"中国北京朝阳\",\n"
				+ "        \"id\": \"63\",\n"
				+ "        \"path1\": \"20151117/564aa3d635f74.png\",\n"
				+ "        \"path2\": \"20151117/564aa3d637268.png\",\n"
				+ "        \"path3\": null,\n" + "        \"path4\": null,\n"
				+ "        \"path5\": null,\n" + "        \"path6\": null,\n"
				+ "        \"path7\": null,\n" + "        \"path8\": null,\n"
				+ "        \"path9\": null,\n"
				+ "        \"content\": \"太好吃了\",\n"
				+ "        \"type\": \"2\",\n" + "        \"imgnum\": \"2\",\n"
				+ "        \"count\": \"1\",\n"
				+ "        \"c_time\": \"1447732182\",\n"
				+ "        \"special\": null,\n" + "        \"cnum\": \"0\",\n"
				+ "        \"gz\": 3,\n" + "        \"dz\": 3,\n"
				+ "        \"sc\": 3,\n" + "        \"sid\": null,\n"
				+ "        \"labels\": [\n" + "            {\n"
				+ "                \"lname\": \"粉丝\",\n"
				+ "                \"lid\": \"57\"\n" + "            },\n"
				+ "            {\n" + "                \"lname\": \"酸菜\",\n"
				+ "                \"lid\": \"58\"\n" + "            }\n"
				+ "        ]\n" + "    },\n" + "    \"info\": [\n"
				+ "        [\n" + "            {\n"
				+ "                \"lid\": \"56\",\n"
				+ "                \"lname\": \"好好吃\",\n"
				+ "                \"xaxis\": \"0.5989583134651184\",\n"
				+ "                \"yaxis\": \"0.3119140565395355\",\n"
				+ "                \"direct\": \"left\",\n"
				+ "                \"lclass\": \"zl\"\n" + "            }\n"
				+ "        ],\n" + "        [\n" + "            {\n"
				+ "                \"lid\": null,\n"
				+ "                \"lname\": null,\n"
				+ "                \"xaxis\": null,\n"
				+ "                \"yaxis\": null,\n"
				+ "                \"direct\": null,\n"
				+ "                \"lclass\": null\n" + "            }\n"
				+ "        ]\n" + "    ],\n" + "    \"status\": 1\n" + "}";
		try {
			// 第一层解析
			JSONObject jsonObject = new JSONObject(jsonString);
			JSONObject dataObject = jsonObject.getJSONObject("data");
			JSONArray infoArray = jsonObject.getJSONArray("info");
			int status = jsonObject.getInt("status");
			System.out.println("status=" + status);

			// 第二层解析
			// 解析data
			int uid = dataObject.getInt("uid");
			System.out.println("uid=" + uid);
			// data中其他属性类同
			JSONArray lablesArray = dataObject.getJSONArray("labels");
			for (int i = 0; i < lablesArray.length(); i++) {
				JSONObject childJsonObject = lablesArray.getJSONObject(i);
				String lid = childJsonObject.getString("lid");
				String lname = childJsonObject.getString("lname");
				System.out.println("lables lid=" + lid);
				System.out.println("lables lname=" + lname);
			}

			// 解析info
			for (int i = 0; i < infoArray.length(); i++) {
				JSONObject childJsonObject = infoArray.getJSONArray(i)
						.getJSONObject(0);
				String lid = childJsonObject.getString("lid");
				String lname = childJsonObject.getString("lname");
				// info中其他属性类同
				System.out.println("info lid=" + lid);
				System.out.println("info lname=" + lname);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
11-17 19:49:55.130: I/System.out(5580): uid=213
11-17 19:49:55.130: I/System.out(5580): lables lid=57
11-17 19:49:55.130: I/System.out(5580): lables lname=粉丝
11-17 19:49:55.130: I/System.out(5580): lables lid=58
11-17 19:49:55.130: I/System.out(5580): lables lname=酸菜
11-17 19:49:55.130: I/System.out(5580): info lid=56
11-17 19:49:55.130: I/System.out(5580): info lname=好好吃
11-17 19:49:55.130: I/System.out(5580): info lid=null
11-17 19:49:55.130: I/System.out(5580): info lname=null
阿飞__ 2015-11-18
  • 打赏
  • 举报
回复
把data后面的大括号里的所有元素可以提取成一个对象bean,用fastJson/GSON直接解析成对象 info同理,可以直接解析成一个对象的数组
sanxiaochengyu 2015-11-18
  • 打赏
  • 举报
回复
key都固定的吧?一个jsonobject.里面三个元素data info status,data里面有个labels包含两个元素的JSONArray,info里面包含两个元素的JSONArray

80,492

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧