json数据转java对象(复杂java对象)

来源:互联网 发布:卡通人物制作软件 编辑:程序博客网 时间:2024/05/16 08:26

直接贴代码:

主要代码:

package json;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONArray;import net.sf.json.JSONObject;/** * 要想实现JSON和java对象之间的互转,需要借助第三方jar包,这里使用json-lib这个jar包, * json-lib需要commons-beanutils、commons-collections、commons-lang、 * commons-logging、ezmorph五个包的支持. * json-lib提供了几个类可以完成此功能,例,JSONObject、JSONArray。 * 从类的名字上可以看出JSONObject转化的应该是对象格式的,而JSONArray转化的则应该是数组对象(即,带[]形式)的。 * @author hg * */@SuppressWarnings("rawtypes")public class JsonHandle {    public static void main(String[] args) {        JsonHandle jsonHandle = new JsonHandle();        jsonHandle.JsonArrayDemo();        jsonHandle.JsonObjectDemo();    }    /**     * JSON 数组对象格式 [ 开始的json数据     */    public void JsonArrayDemo(){        String jsonStr = "[{\"name\":\"小贺\",\"age\":20,\"sex\":\"男\","                + "\"pictures\":[{\"fileName\":\"1.jpg\",\"pictureUrl\":\"http://www.1.jsp\"},"                + "{\"fileName\":\"2.jpg\",\"pictureUrl\":\"http://www.2.jsp\"}]},"                + "{\"name\":\"小露\",\"age\":19,\"sex\":\"女\","                + "\"pictures\":[{\"fileName\":\"3.jpg\",\"pictureUrl\":\"http://www.3.jsp\"},"                + "{\"fileName\":\"4.jpg\",\"pictureUrl\":\"http://www.4.jsp\"}]}]";        //数组对象格式使用JSONArray        JSONArray jsonArray = JSONArray.fromObject(jsonStr);        //对应json的数组 List<PersonalInfo>        List<PersonalInfo> personalInfos =new ArrayList<PersonalInfo>();                //遍历 jsonArray        for(int i=0;i<jsonArray.size();i++){            //获得jsonArray的第 i 个元素            Object o = jsonArray.get(i);            //转换为单个 JSONObject 对象            JSONObject jsonObject = JSONObject.fromObject(o);            /*             * 当其中属性有类似List, Map,ArrayList就不可以了。 会报错:MorphDynaBean cannot be               * cast to ******。             * 在JSONObject.toBean的时候如果转换的类中有集合,             * 可以先定义    Map<String, Class<E>> classMap = new HashMap<String, Class<E>>();             * 在classMap中put你要转换的类中的集合名,比如:classMap.put("pictures", Picture.class);             * 然后在toBean()的时候把参数加上,             * 例如:PersonalInfo personalInfo =              * (PersonalInfo)JSONObject.toBean(jsonObject, PersonalInfo.class,classMap);             */            Map<String, Class> pictureMap = new HashMap<String, Class>();            pictureMap.put("pictures",Picture.class);            //转化为java对象            PersonalInfo personalInfo =                     (PersonalInfo)JSONObject.toBean(jsonObject, PersonalInfo.class,pictureMap);            //添加对象至数组            personalInfos.add(personalInfo);        }        for(PersonalInfo pi : personalInfos){            //对象toString()打印:PersonalInfo {name=小贺, age=20, sex=男, pictures=            //[{fileName=1.jpg, pictureUrl=http://www.1.jsp},            //{fileName=2.jpg, pictureUrl=http://www.2.jsp}]}            System.out.println(pi.toString());        }    }    /**     * JSON的对象格式的字符串  { 开始的json数据     */    public void JsonObjectDemo(){        String jsonStr = "{\"name\":\"小贺\",\"age\":20,\"sex\":\"男\","                + "\"pictures\":[{\"fileName\":\"1.jpg\",\"pictureUrl\":\"http://www.1.jsp\"},"                + "{\"fileName\":\"2.jpg\",\"pictureUrl\":\"http://www.2.jsp\"}]}";        //1、使用JSONObject        JSONObject jsonObject=JSONObject.fromObject(jsonStr);        Map<String, Class> pictureMap = new HashMap<String, Class>();        pictureMap.put("pictures",Picture.class);        //转化为java对象        PersonalInfo personalInfo =                 (PersonalInfo)JSONObject.toBean(jsonObject, PersonalInfo.class,pictureMap);        //对象toString()打印:PersonalInfo {name=小贺, age=20, sex=男,         //pictures=[{fileName=1.jpg, pictureUrl=http://www.1.jsp},        //{fileName=2.jpg, pictureUrl=http://www.2.jsp}]}        System.out.println(personalInfo.toString());    }}

PersonalInfo 实体类 :

package json;import java.util.List;/** * 个人信息 * @author hg * */public class PersonalInfo {    private String name;            //姓名    private Integer age;            //年龄    private String sex;             //性别    private List<Picture> pictures; //照片    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public List<Picture> getPictures() {        return pictures;    }    public void setPictures(List<Picture> pictures) {        this.pictures = pictures;    }    public String toString() {        String picturesStr = "";        for(int i=0;i<pictures.size();i++){            picturesStr+=pictures.get(i).toString();            if(i<pictures.size()-1){                picturesStr+=",";            }        }        return "PersonalInfo {name=" + name + ", age="         + age + ", sex=" + sex + ", pictures=[" + picturesStr+ "]}";    }}

Picture 实体类

package json;/** * 个人照片Entity * @author hg * */public class Picture {    private String fileName;    //文件名    private String pictureUrl;  //照片Url    public String getFileName() {        return fileName;    }    public void setFileName(String fileName) {        this.fileName = fileName;    }    public String getPictureUrl() {        return pictureUrl;    }    public void setPictureUrl(String pictureUrl) {        this.pictureUrl = pictureUrl;    }    public String toString() {        return "{fileName=" + fileName + ", pictureUrl=" + pictureUrl + "}";    }}

对象型json数据

{    "name":"小贺",    "age":20,    "sex":"男",    "pictures":[        {            "fileName":"1.jpg",            "pictureUrl":"http://www.1.jsp"        },        {            "fileName":"2.jpg",            "pictureUrl":"http://www.2.jsp"        }    ]}

数组型json数据

[    {        "name":"小贺",        "age":20,        "sex":"男",        "pictures":[            {                "fileName":"1.jpg",                "pictureUrl":"http://www.1.jsp"            },            {                "fileName":"2.jpg",                "pictureUrl":"http://www.2.jsp"            }        ]    },    {        "name":"小露",        "age":19,        "sex":"女",        "pictures":[            {                "fileName":"3.jpg",                "pictureUrl":"http://www.3.jsp"            },            {                "fileName":"4.jpg",                "pictureUrl":"http://www.4.jsp"            }        ]    }]