fastjson 使用时遇到的问题

今天服务端又修改了接口,导致了本地的解析json的工具类不能使用,本来以为,小改一下就ok了,看到服务端返回的结果傻眼了.下面以小Demo的方式进行说明,方便自己以后查看.

先看看服务端返回的json格式类型

{
    "users": [
        {
            "id": "10001",
            "name": "张三",
            "age": 20,
            "address": "AAAAAAAAAAAAAAAAAAAAAAAAAAA",
            "images": [
                {
                    "title": "A头像1",
                    "url": "http://192.168.191.202/images/01.png",
                    "desc": "用户第一个头像"
                },
                {
                    "title": "A头像2",
                    "url": "http://192.168.191.202/images/02.png",
                    "desc": "用户第二个头像"
                },
                {
                    "title": "A头像3",
                    "url": "http://192.168.191.202/images/03.png",
                    "desc": "用户第三个头像"
                }
            ]
        },
        {
            "id": "10002",
            "name": "李四",
            "age": 22,
            "address": "BBBBBBBBBBBBBBBBBBBBBBBBB",
            "images": [
                {
                    "title": "B头像1",
                    "url": "http://192.168.191.202/images/11.png",
                    "desc": "用户第一个头像"
                },
                {
                    "title": "B头像2",
                    "url": "http://192.168.191.202/images/12.png",
                    "desc": "用户第二个头像"
                },
                {
                    "title": "B头像2",
                    "url": "http://192.168.191.202/images/12.png",
                    "desc": "用户第二个头像"
                },
                {
                    "title": "B头像3",
                    "url": "http://192.168.191.202/images/13.png",
                    "desc": "用户第三个头像"
                }
            ]
        }
    ]
}
由于刚出道不久,也第一次在实际中遇到这样的json,本以为利用fastjson封装成相关的bean对象就可以解决了.

User对象:

package com.example.fastjsontest;

import java.util.List;

public class User {
	private int id;
	private String name;
	private int age;
	private String address;
	private List images;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public List getImages() {
		return images;
	}
	public void setImages(List images) {
		this.images = images;
	}
	
}
Images对象:

package com.example.fastjsontest;

public class Images {
	private String title;
	private String url;
	private String desc;
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	
}

然后利用fastjson的方法:JSON.parseArray(node, clazz);deBug 发现返回的List为空,并抛出了com.alibaba.fastjson.JSONException: default constructor not found..上网查了一下,原来fastjson bean对象 包含非java自定义类型,好像jdk 8.0的一个日期类型也不支持.最后无奈,只好自己手动解析各个节点,通过set方法进行设值.

感觉到自己写的这个json解析的效率不高,但是fastjson也不能用,可能是还不知道要怎么用.如果大神们有什么建议或者方案,小弟感恩不尽!




你可能感兴趣的:(Android,问题)