首页 > Web开发 > 详细

Json转list,两种包,两种方式

时间:2014-08-14 20:02:09      阅读:357      评论:0      收藏:0      [点我收藏+]

1、使用fastjson

对于json串大小写没什么要求,测试的时候,我把javaBean属性设置成和json串一样的大小写,代码如下:

package com.myTest.json.test1;

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

import com.alibaba.fastjson.JSON;

public class app1 {

    public static void main(String[] args) {
        String jsonString = "[{‘Value‘:‘0‘,‘Name‘:‘测试0‘,‘statusInfo‘:{‘Status‘:‘0‘}},{‘Value‘:‘1‘,‘Name‘:‘测试1‘,‘statusInfo‘:{‘Status‘:‘10‘}},{‘Value‘:‘2‘,‘Name‘:‘测试2‘,‘statusInfo‘:{‘Status‘:‘20‘}},{‘Value‘:‘3‘,‘Name‘:‘测试3‘,‘statusInfo‘:{‘Status‘:‘30‘}},{‘Value‘:‘4‘,‘Name‘:‘测试4‘,‘statusInfo‘:{‘Status‘:‘40‘}},{‘Value‘:‘5‘,‘Name‘:‘测试5‘,‘statusInfo‘:{‘Status‘:‘50‘}},{‘Value‘:‘6‘,‘Name‘:‘测试6‘,‘statusInfo‘:{‘Status‘:‘60‘}},{‘Value‘:‘7‘,‘Name‘:‘测试7‘,‘statusInfo‘:{‘Status‘:‘70‘}},{‘Value‘:‘8‘,‘Name‘:‘测试8‘,‘statusInfo‘:{‘Status‘:‘80‘}},{‘Value‘:‘9‘,‘Name‘:‘测试9‘,‘statusInfo‘:{‘Status‘:‘90‘}}]";
        //String jsonString = "{‘Value‘:‘0‘,‘Name‘:‘测试0‘,‘statusInfo‘:{‘Status‘:‘12‘}}";
        List<DeviceReturn> deviceReturns= app1.getPersons(jsonString, DeviceReturn.class);
        System.out.println(deviceReturns);
        String toJsonString = JSON.toJSONString(deviceReturns);
        System.out.println(toJsonString);
        
    }

    public static <T> T getPerson(String jsonString, Class cls) {
        T t = null;
        try {
            t = (T) JSON.parseObject(jsonString, cls);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return t;
    }
    
    public static <T> List<T> getPersons(String jsonString, Class cls) {
        List<T> list = new ArrayList<T>();
        try {
            list = JSON.parseArray(jsonString, cls);
        } catch (Exception e) {
        }
        return list;
    }
}

结果如下:

[com.myTest.json.test1.DeviceReturn@25d285b, com.myTest.json.test1.DeviceReturn@32046f93, com.myTest.json.test1.DeviceReturn@4139358c, com.myTest.json.test1.DeviceReturn@12ea9bc8, com.myTest.json.test1.DeviceReturn@6899712b, com.myTest.json.test1.DeviceReturn@31d4f3b3, com.myTest.json.test1.DeviceReturn@13cb8654, com.myTest.json.test1.DeviceReturn@16dffef3, com.myTest.json.test1.DeviceReturn@4bdb0f40, com.myTest.json.test1.DeviceReturn@1ac659a8]
[{"name":"测试0","statusInfo":{"status":"0"},"value":"0"},{"name":"测试1","statusInfo":{"status":"10"},"value":"1"},{"name":"测试2","statusInfo":{"status":"20"},"value":"2"},{"name":"测试3","statusInfo":{"status":"30"},"value":"3"},{"name":"测试4","statusInfo":{"status":"40"},"value":"4"},{"name":"测试5","statusInfo":{"status":"50"},"value":"5"},{"name":"测试6","statusInfo":{"status":"60"},"value":"6"},{"name":"测试7","statusInfo":{"status":"70"},"value":"7"},{"name":"测试8","statusInfo":{"status":"80"},"value":"8"},{"name":"测试9","statusInfo":{"status":"90"},"value":"9"}]

json串中大写的,转成javaBean,再转成Json串后,变成符合java规范的属性名,很强大的一个包,赞一个。

2、使用net.sf.json.JSON

json串必须是符合java规范的属性,首字母必须小写,否则不能转成javaBean

public static void main(String[] args) {
        String jsonString = "[{\"value\":\"0\",\"name\":\"测试0\",\"statusInfo\":{\"status\":\"0\"}},{\"value\":\"1\",\"name\":\"测试1\",\"statusInfo\":{\"status\":\"10\"}}]";
        JSONArray array = JSONArray.fromObject(jsonString);
        System.out.println("JSONArray:"+array);
        List<?> deviceReturns = JSONArray.toList(array, new DeviceReturn(), new JsonConfig());
        JSONObject ob = JSONObject.fromObject(deviceReturns.get(0));
        System.out.println(ob);
    }

修改javaBean与之对应,也能成功。

Json转list,两种包,两种方式,布布扣,bubuko.com

Json转list,两种包,两种方式

原文:http://www.cnblogs.com/winkey4986/p/3913085.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!