Json与bean的相互转换

本文使用json-lib jar包实现Json与bean的相互转换


1.将字符串转为JSON

使用JSONObject.fromObject(str)方法即可将字符串转为JSON对象

使用JSONObject.put("attribute","value")可为JSON添加属性

如果需要转为JSON数组,只需使用JSONArray对象提供的方法即可

  /**
     * 一些简单的转换
     */
    public static void transformStringTest() {
        String str = "{" + "\"id\":" + "\"1\"," + "\"name\":" + "\"zhangsan\""
                + "}";
        //1.将字符串转为JSON
        JSONObject jsonObj = JSONObject.fromObject(str);
        System.out.println(jsonObj.toString());
        //JSON添加属性
        jsonObj.put("age", "22");
        System.out.println(jsonObj.toString());
        //2.将对象转为数组
        JSONArray jsonArr = JSONArray.fromObject(jsonObj);
        System.out.println(jsonArr.toString());
        //3.将数组添加到JSON对象中
        JSONObject obj = new JSONObject();
        obj.put("employees", jsonArr);
        System.out.println(obj.toString());
    }
    /* 输出内容
     * {"id":"1","name":"zhangsan"} 
     * {"id":"1","name":"zhangsan","age":"22"}
     * [{"id":"1","name":"zhangsan","age":"22"}]
     * {"employees":[{"id":"1","name":"zhangsan","age":"22"}]}
     */


2.将对象转为JSON

首先创建People类

public class People {
    private String name;

    private String idcard;

    public People() {
    }

    public People(String name, String idcard) {
        this.name = name;
        this.idcard = idcard;
    }

    public String getName() {
        return name;
    }

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

    public String getIdcard() {
        return idcard;
    }

    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }

}

将对象转为JSON同样使用SONObject.fromObject(obj)方法

如果是一个List,转为JSON时需要使用JSONArray将对象转为JSON数组

public static void transformObjectTest() {
        People p1 = new People("a", "111111");
        //1.将对象转为json
        System.out.println(JSONObject.fromObject(p1));
        List<People> peopleList = new ArrayList<People>();
        peopleList.add(p1);
        //2.将list转为json(需要使用数组JSONArray)
        JSONArray arr = JSONArray.fromObject(peopleList);
        System.out.println(arr.toString());
    } 

 /*输出内容
     * {"idcard":"111111","name":"a"} 
     * [{"idcard":"111111","name":"a"}]
     */

3.JSON转为bean

json转为bean的方法也非常简单,只需使用JSONObject.toBean()方法即可,使用该方法的时候需要传入Bean的class

/**
     * 将json转换为bean
     * @param json
     * @param type
     * @return
     */
    public static <T> Object transformJsonToBean(String json, Class<T> type) {
        JSONObject jsonObject = JSONObject.fromObject(json);
        return JSONObject.toBean(jsonObject, type);
    }


4.JSON转为list<bean>集合

由于是集合,所以需要使用JSONArray,JSONArray提供了toCollection方法,使用该方法同样需要传入bean的class

 public static <T> Object transformJsonToBeanList(String jsonArr,
            Class<T> type) {
        JSONArray jsonArray = JSONArray.fromObject(jsonArr);
        return JSONArray.toCollection(jsonArray, type);
    }


5.测试

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

        //transformStringTest();
        //transformObjectTest();
        People p1 = new People("a", "111111");
        String json = JSONObject.fromObject(p1).toString();
        //将json转换为bean
        People p = (People) transformJsonToBean(json, People.class);
        System.out.println(p.getName());
        System.out.println(p.getIdcard());

        List<People> peopleList = new ArrayList<People>();
        peopleList.add(p1);
        JSONArray jsonArr = JSONArray.fromObject(peopleList);
        //将json数组转为list
        List<People> list = (List<People>) transformJsonToBeanList(
            jsonArr.toString(), People.class);
        System.out.println(list.size());

    }

代码下载:http://download.csdn.net/detail/lom9357bye/9690304


  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值