首页 > 代码库 > Jason 和 Java 对象转化示例

Jason 和 Java 对象转化示例

1.工程

2.代码:

JsonUtil.java

package com.my.json;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUtil {

    /**
     * 从一个JSON 对象字符格式中得到一个java对象
     * 
     * @param jsonString
     *            json字符串
     * 
     * @param pojoCalss
     *            java对象的class
     * 
     * @return
     */

    public static Object jsonString2Object(String jsonString, Class pojoCalss) {

        Object pojo;

        JSONObject jsonObject = JSONObject.fromObject(jsonString);

        pojo = JSONObject.toBean(jsonObject, pojoCalss);

        return pojo;

    }

    /**
     * 
     * 将java对象转换成json字符串<br>
     * 支持复杂类型:Java bean 中既有属性又有list
     * 
     * @param javaObj
     * 
     * @return
     */

    public static String object2JsonString(Object javaObj) {

        JSONObject json;

        json = JSONObject.fromObject(javaObj);

        return json.toString();

    }



    /**
     * 
     * 从json对象集合表达式中得到一个java对象列表
     * 
     * @param jsonString
     * 
     * @param pojoClass
     * 
     * @return
     */

    public static List jsonList2JavaList(String jsonString, Class pojoClass) {

        JSONArray jsonArray = JSONArray.fromObject(jsonString);

        JSONObject jsonObject;

        Object pojoValue;

        List list = new ArrayList();

        for (int i = 0; i < jsonArray.size(); i++) {

            jsonObject = jsonArray.getJSONObject(i);

            pojoValue = JSONObject.toBean(jsonObject, pojoClass);

            list.add(pojoValue);

        }

        return list;

    }

    /**
     * 
     * 从json对象集合表达式中得到一个java对象列表
     * 
     * @param jsonString
     * 
     * @param pojoClass
     * 
     * @return
     */

    public static String javaList2JsonList(List list) {

        JSONArray jsonArray = JSONArray.fromObject(list);
        return jsonArray.toString();
    }

}

Person.java

package com.my.json;

import java.util.ArrayList;
import java.util.List;

public class Person {

    private String name;

    private List<Person> list = new ArrayList<Person>();

    public List<Person> getList() {
        return list;
    }

    public void setList(List<Person> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", num=" + num + ", sex=" + sex
                + ", age=" + age + "]";
    }

    private int num;
    private String sex;
    private int age;

    public Person() {
        // TODO Auto-generated constructor stub
    }

    public Person(String name, int num, String sex, int age) {
        super();
        this.name = name;
        this.num = num;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

Test.java

package com.my.json;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

public class Test {
    public static void main(String[] args) {

        // 将一个java对象转换为Json字符串
        Person p = new Person("gu1", 123, "male", 23);
        String str = JsonUtil.object2JsonString(p);
        System.out.println(str);

        // 将一个JSON字符串转化为Java对象
        String s = "{\"age\":23,\"name\":\"gu1\",\"num\":123,\"sex\":\"male\"}";
        Person p2 = (Person) JsonUtil.jsonString2Object(s, Person.class);
        System.out.println(p2);

        // 将一个对象列表转化为JSON列表
        List<Person> list = new ArrayList<Person>();
        for (int i = 0; i < 5; i++) {
            Person persion = new Person("zhang" + i, 1 + i, "man" + i, 1 + i);
            list.add(persion);
        }
        String jsonList = JsonUtil.javaList2JsonList(list);
        System.out.println(jsonList);

        // 将一个JSON列表转化为Java列表
        String jst = "[{\"age\":1,\"list\":[],\"name\":\"zhang0\",\"num\":1,\"sex\":\"man0\"},{\"age\":2,\"list\":[],\"name\":\"zhang1\",\"num\":2,\"sex\":\"man1\"},{\"age\":3,\"list\":[],\"name\":\"zhang2\",\"num\":3,\"sex\":\"man2\"},{\"age\":4,\"list\":[],\"name\":\"zhang3\",\"num\":4,\"sex\":\"man3\"},{\"age\":5,\"list\":[],\"name\":\"zhang4\",\"num\":5,\"sex\":\"man4\"}]";
        List<Person> l = (List<Person>) JsonUtil.jsonList2JavaList(jst,
                Person.class);
        System.out.println(l.size());

        // 复杂类型转JSON:Java bean 中既有属性又有list(项目中经常见到)
        List<Person> list2 = new ArrayList<Person>();
        for (int i = 0; i < 10; i++) {
            Person persion = new Person("zhang" + i, 1 + i, "man" + i, 1 + i);
            list2.add(persion);
        }
        Person me = new Person("zhang", 1, "man", 1);
        me.setList(list2);

        String str2 = JsonUtil.object2JsonString(me);
        System.out.println(str2);

        // 复杂类型转Java:JSON 中既有属性又有list(项目中经常见到)
        String ss = "{\"age\":1,\"list\":[{\"age\":1,\"list\":[],\"name\":\"zhang0\",\"num\":1,\"sex\":\"man0\"},{\"age\":2,\"list\":[],\"name\":\"zhang1\",\"num\":2,\"sex\":\"man1\"},{\"age\":3,\"list\":[],\"name\":\"zhang2\",\"num\":3,\"sex\":\"man2\"},{\"age\":4,\"list\":[],\"name\":\"zhang3\",\"num\":4,\"sex\":\"man3\"},{\"age\":5,\"list\":[],\"name\":\"zhang4\",\"num\":5,\"sex\":\"man4\"},{\"age\":6,\"list\":[],\"name\":\"zhang5\",\"num\":6,\"sex\":\"man5\"},{\"age\":7,\"list\":[],\"name\":\"zhang6\",\"num\":7,\"sex\":\"man6\"},{\"age\":8,\"list\":[],\"name\":\"zhang7\",\"num\":8,\"sex\":\"man7\"},{\"age\":9,\"list\":[],\"name\":\"zhang8\",\"num\":9,\"sex\":\"man8\"},{\"age\":10,\"list\":[],\"name\":\"zhang9\",\"num\":10,\"sex\":\"man9\"}],\"name\":\"zhang\",\"num\":1,\"sex\":\"man\"}";
        Map map = (Map)JSONObject.fromObject(ss); 
        
        System.out.println("------------------------------");
        System.out.println(map);
        String name = (String)map.get("name");
        int num = (Integer)map.get("num");
        String sex = (String)map.get("sex");
        int age = (Integer)map.get("age");
        System.out.println("name="+name);
        System.out.println("num="+"num");
        System.out.println("sex="+"sex");
        System.out.println("age="+"age");
        System.out.println("list="+map.get("list"));
        //获取list字符串,再转化为Java 对象,其实是再次构建成Java bean
        String listStr = map.get("list").toString();//一定要用toString,不能强转
        List<Person> ll = JsonUtil.jsonList2JavaList(listStr, Person.class);
        //构建新的bean:不知是否有其他方案,根据需要是否构建
        Person pp = new Person(name,num,sex,age);
        pp.setList(ll);
        //输出bean
        for (Person ps : pp.getList())
        {
            System.out.println(ps.getName());
        }
            
    }
}

输出:

{"age":23,"list":[],"name":"gu1","num":123,"sex":"male"}
Person [name=gu1, num=123, sex=male, age=23]
[{"age":1,"list":[],"name":"zhang0","num":1,"sex":"man0"},{"age":2,"list":[],"name":"zhang1","num":2,"sex":"man1"},{"age":3,"list":[],"name":"zhang2","num":3,"sex":"man2"},{"age":4,"list":[],"name":"zhang3","num":4,"sex":"man3"},{"age":5,"list":[],"name":"zhang4","num":5,"sex":"man4"}]
5
{"age":1,"list":[{"age":1,"list":[],"name":"zhang0","num":1,"sex":"man0"},{"age":2,"list":[],"name":"zhang1","num":2,"sex":"man1"},{"age":3,"list":[],"name":"zhang2","num":3,"sex":"man2"},{"age":4,"list":[],"name":"zhang3","num":4,"sex":"man3"},{"age":5,"list":[],"name":"zhang4","num":5,"sex":"man4"},{"age":6,"list":[],"name":"zhang5","num":6,"sex":"man5"},{"age":7,"list":[],"name":"zhang6","num":7,"sex":"man6"},{"age":8,"list":[],"name":"zhang7","num":8,"sex":"man7"},{"age":9,"list":[],"name":"zhang8","num":9,"sex":"man8"},{"age":10,"list":[],"name":"zhang9","num":10,"sex":"man9"}],"name":"zhang","num":1,"sex":"man"}
------------------------------
{"age":1,"list":[{"age":1,"list":[],"name":"zhang0","num":1,"sex":"man0"},{"age":2,"list":[],"name":"zhang1","num":2,"sex":"man1"},{"age":3,"list":[],"name":"zhang2","num":3,"sex":"man2"},{"age":4,"list":[],"name":"zhang3","num":4,"sex":"man3"},{"age":5,"list":[],"name":"zhang4","num":5,"sex":"man4"},{"age":6,"list":[],"name":"zhang5","num":6,"sex":"man5"},{"age":7,"list":[],"name":"zhang6","num":7,"sex":"man6"},{"age":8,"list":[],"name":"zhang7","num":8,"sex":"man7"},{"age":9,"list":[],"name":"zhang8","num":9,"sex":"man8"},{"age":10,"list":[],"name":"zhang9","num":10,"sex":"man9"}],"name":"zhang","num":1,"sex":"man"}
name=zhang
num=num
sex=sex
age=age
list=[{"age":1,"list":[],"name":"zhang0","num":1,"sex":"man0"},{"age":2,"list":[],"name":"zhang1","num":2,"sex":"man1"},{"age":3,"list":[],"name":"zhang2","num":3,"sex":"man2"},{"age":4,"list":[],"name":"zhang3","num":4,"sex":"man3"},{"age":5,"list":[],"name":"zhang4","num":5,"sex":"man4"},{"age":6,"list":[],"name":"zhang5","num":6,"sex":"man5"},{"age":7,"list":[],"name":"zhang6","num":7,"sex":"man6"},{"age":8,"list":[],"name":"zhang7","num":8,"sex":"man7"},{"age":9,"list":[],"name":"zhang8","num":9,"sex":"man8"},{"age":10,"list":[],"name":"zhang9","num":10,"sex":"man9"}]
zhang0
zhang1
zhang2
zhang3
zhang4
zhang5
zhang6
zhang7
zhang8
zhang9