Json 和复杂 Bean 之间的转化

使用json-lib

如果不使用maven 需要下面4个jar

commons-beanutils-1.8.3.jar

commons-lang-2.3.jar

ezmorph-1.0.6.jar

json-lib-2.4-jdk15.jar

使用maven 

Pom.xml

        <!--Start json-->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
          <span style="color:#ff0000;">  <classifier>jdk15</classifier></span>
        </dependency>

json-lib  有jdk13 和jdk15 两个版本,使用时请指明使用哪个版本。

package json;

/**
 * Created with IntelliJ IDEA.
 * User: Alvin
 * Date: 14-8-6
 * Time: 上午10:24
 * To change this template use File | Settings | File Templates.
 */
import bean.Student;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import bean.Dormitory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonUtil {
    /*
     *  普通类型、List、Collection等都是用JSONArray解析
     *
     *  Map、自定义类型是用JSONObject解析
     *  可以将Map理解成一个对象,里面的key/value对可以理解成对象的属性/属性值
     *  即{key1:value1,key2,value2......}
     *
     * 1.JSONObject是一个name:values集合,通过它的get(key)方法取得的是key后对应的value部分(字符串)
     *         通过它的getJSONObject(key)可以取到一个JSONObject,--> 转换成map,
     *         通过它的getJSONArray(key) 可以取到一个JSONArray ,
     *
     *
     */

    //一般数组转换成JSON
    public String Array2Json(){
        String[] array ={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
        JSONArray jsonArray = JSONArray.fromObject(array);
        return jsonArray.toString();
    }

    //Collection对象转换成JSON
    public String list2Json(){
        List list = new ArrayList();
        list.add( "first" );
        list.add( "second" );
        JSONArray jsonArray = JSONArray.fromObject( list );
        return jsonArray.toString();
    }

    //Map转换成json, 是用jsonObject
    public String map2Json(){
        Map map = new HashMap();
        map.put( "name", "json" );
        map.put( "bool", Boolean.TRUE );
        map.put( "int", new Integer(1) );
        map.put( "arr", new String[]{"a","b"} );
        map.put( "func", "function(i){ return this.arr[i]; }" );
        JSONObject jsonObject = JSONObject.fromObject(map);
        return jsonObject.toString();
    }


    // Bean 2 Json
    public String bean2Json(){
        List<Student> students = new ArrayList<Student>();
        Student st1 = new Student(2111212049,"张三",25,"alvalp@live.com");
        Student st2 = new Student(2111212049,"李四",25,"alvalp@live.com");
        Student st3 = new Student(2111212049,"王五",25,"alvalp@live.com");
        Student st4 = new Student(2111212049,"赵六",25,"alvalp@live.com");
        students.add(st1);
        students.add(st2);
        students.add(st3);
        students.add(st4);
        Dormitory dormitory = new Dormitory("东13#",206,students);

        JSONObject jsonObject = JSONObject.fromObject(dormitory);
        return jsonObject.toString();
    }

    public Object  json2Bean(String json){
        JSONObject jsonObject  = JSONObject.fromObject(json);
        Dormitory dormitory = null;
        if(jsonObject != null) {
<span style="color:#ff0000;">            Map<String, Class> m = new HashMap<String, Class>();
            m.put("students", Student.class);</span>
            dormitory = (Dormitory) jsonObject.toBean(jsonObject,Dormitory.class,m);
        }
        return dormitory;
    }





}



package bean;

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

/**
 * Created with IntelliJ IDEA.
 * User: Alvin
 * Date: 14-8-6
 * Time: 上午10:46
 * To change this template use File | Settings | File Templates.
 */
public class Dormitory {
    private String address;
    private int num;
    private Student student = new Student();

    private List<Student> students ;


    public Dormitory() {
    }

    public Dormitory(String address, int num, List<Student> students) {
        this.address = address;
        this.num = num;
        this.students = students;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getNum() {
        return num;
    }

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

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }
}

package bean;

/**
 * Created with IntelliJ IDEA.
 * User: Alvin
 * Date: 14-8-1
 * Time: 下午4:13
 * To change this template use File | Settings | File Templates.
 */
public class Student {
    private int num;
    private String name;
    private int age;
    private String email;

    public Student() {
    }

    public Student(int num, String name, int age, String email) {
        this.num = num;
        this.name = name;
        this.age = age;
        this.email = email;
    }

    public int getNum() {
        return num;
    }

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

    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 getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}



复杂bean 的转化 注意如下几点

1。为转化的类构建无参的构造方法;

2.注意上面红色部分代码,需要为复杂bean 里面的bean传入bean的类型。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值