阿里巴巴Json工具-Fastjson讲解

时间:2024-03-09 14:27:19

Fastjson是阿里巴巴公司开源的速度最快的Json和对象转换工具,一个Java语言编写的JSON处理器。

1、遵循http://json.org标准,为其官方网站收录的参考实现之一。
2、功能qiang打,支持JDK的各种类型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。
3、无依赖,不需要例外额外的jar,能够直接跑在JDK上。
4、开源,使用Apache License 2.0协议开源。http://code.alibabatech.com/wiki/display/FastJSON/Home

2、主要的使用入口

Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray 
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject    
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean 
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合 
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 
public static final Object toJSON(Object javaObject); //将JavaBean转换为JSONObject或者JSONArray。
 
//***************************
JSONObject  json = JSON.parseObject(resultScheduleRedis);
JSONArray jsonArrayResultRedis = JSONArray.parseArray(String.valueOf(json.get("result")));



json转 对象
ScheduleDTO resultDTO = JSON.parseObject(json.toJSONString(), new TypeReference<ScheduleDTO>() {});

//**************************************************

 
 

3、有关类库的一些说明

SerializeWriter:相当于StringBuffer
JSONArray:相当于List<Object>
JSONObject:相当于Map<String, Object>

JSON反序列化没有真正数组,本质类型都是List<Object>

、fastjson还有很多很高级的特性,比如支持注解、支持全类型序列化,这些都是很好的特性,功能强大,不再讲解。

这是关于FastJson的一个使用Demo,在Java环境下验证的

 
  1. class User{  
  2.     private int id;  
  3.     private String name;  
  4.     public int getId() {  
  5.         return id;  
  6.     }  
  7.     public void setId(int id) {  
  8.         this.id = id;  
  9.     }  
  10.     public String getName() {  
  11.         return name;  
  12.     }  
  13.     public void setName(String name) {  
  14.         this.name = name;  
  15.     }  
  16. }  

*******************************************************************************************************************************************

 

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. class Group{  
  5.     private int id;  
  6.     private String name;  
  7.     private List<User> list = new ArrayList<User>();  
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.     public void setId(int id) {  
  12.         this.id = id;  
  13.     }  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     public void setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20.     public List<User> getList() {  
  21.         return list;  
  22.     }  
  23.     public void setList(List<User> list) {  
  24.         this.list = list;  
  25.     }  

******************************************************************************************************************************************************************

  1. lass Weibo {  
  2.     private String id;  
  3.     private String city;  
  4.   
  5.     public Weibo(String id, String city) {  
  6.         this.id = id;  
  7.         this.city = city;  
  8.     }  
  9.   
  10.     public Weibo() {  
  11.     }  
  12.   
  13.     public String getId() {  
  14.         return id;  
  15.     }  
  16.   
  17.     public void setId(String id) {  
  18.         this.id = id;  
  19.     }  
  20.   
  21.     public String getCity() {  
  22.         return city;  
  23.     }  
  24.   
  25.     public void setCity(String city) {  
  26.         this.city = city;  
  27.     }  

******************************************************************************************************************************************************************

  1. import java.util.HashMap;  
  2. import java.util.List;  
  3. import java.util.Map;  
  4.   
  5. import com.alibaba.fastjson.JSON;  
  6. import com.alibaba.fastjson.JSONArray;  
  7. import com.alibaba.fastjson.JSONObject;  
  8. /** 
  9.  * 这是关于FastJson的一个使用Demo,在Java环境下验证的; 
  10.  * 使用的时候可以参照这个例子进行回忆! 
  11.  * 备注:下面写的Json格式的数据"前面的\,意思是告诉编译器忽略掉它后面的"。 
  12.  * 后面定义有三个类:User、Group、Weibo 
  13.  * @author liuzhao 
  14.  */  
  15. public class JsonDemo {  
  16.   
  17.     public static void main(String[] args) {  
  18.           
  19.         json2JsonObjectM1();//将Json文本数据信息转换为JsonObject对象,然后利用键值对的方式获取信息  
  20.         json2BeanM2();//将Json文本数据转换为JavaBean数据!  
  21.         json2JSONArrayM3();//将json数据转化为JSONArray:   
  22.         json2JavaBeanM4();//将JSON文本转换为JavaBean的集合;转换为单纯的1个Object:parseObject(String text, Class<T> clazz);   
  23.         bean2JsonM5();//将JavaBean转换为Json格式的数据/Json文本 用户数据的传递:例如上传服务器  
  24.         bean2JSONObjectM6();//将JavaBean转换为JSON对象  
  25.         complexExampleM7();//一个相对复杂的例子:  
  26.         complex2JsonM8();//一个复杂的Object到Json的Demo  
  27.         complexMap2JsonM9();//将Map数据转换为JsonString的Demo;这个对RedBoy服务端不就直接破了嘛!  
  28.     }  
  29.   
  30.     /** 
  31.      * 将Json文本数据信息转换为JsonObject对象,然后利用键值对的方式获取信息 
  32.      */  
  33.     private static void json2JsonObjectM1() {  
  34.         //一个JsonObject文本数据  
  35.         String s = "{\"name\":\"liuzhao\"}";  
  36.         //将JsonObject数据转换为Json  
  37.         JSONObject object = JSON.parseObject(s);  
  38.         //利用键值对的方式获取到值  
  39.         System.out.println(object.get("name"));  
  40.         /** 
  41.          * 打印: 
  42.          * liuzhao 
  43.          */  
  44.     }  
  45.       
  46.     /** 
  47.      * 将Json文本数据转换为JavaBean数据! 
  48.      * 需要注意的是:这时候的Json文本信息中的键的名称必须和JavaBean中的字段名称一样!键中没有的在这个JavaBean中就显示为null! 
  49.      */  
  50.     private static void json2BeanM2() {  
  51.         String s = "{\"id\":\"0375\",\"city\":\"平顶山\"}";  
  52.         //一个简单方便 的方法将Json文本信息转换为JsonObject对象的同时转换为JavaBean对象!  
  53.         Weibo weibo = JSON.parseObject(s, Weibo.class);//Weibo类在下边定义  
  54.         System.out.println(weibo.getId());  
  55.         System.out.println(weibo.getCity());  
  56.         //打印的结果     0375  
  57.         //              平顶山  
  58.     }  
  59.   
  60.   
  61.     /** 
  62.      * 将Map类型的数据转换为JsonString 
  63.      */  
  64.     private static void complexMap2JsonM9() {  
  65.         Group group = new Group();  
  66.         group.setId(1);  
  67.         group.setName("group");  
  68.           
  69.         User user1 = new User();  
  70.         user1.setId(2);  
  71.         user1.setName("user1");  
  72.   
  73.         User user2 = new User();  
  74.         user2.setId(3);  
  75.         user2.setName("user2");  
  76.         group.getList().add(user1);  
  77.         group.getList().add(user2);  
  78.           
  79.         Map<Integer, Object> map = new HashMap<Integer,Object>();  
  80.         map.put(1, "No.1");  
  81.         map.put(2, "No.2");  
  82.         map.put(3, group.getList());  
  83.           
  84.         String jsonString = JSON.toJSONString(map);  
  85.         System.out.println(jsonString);  
  86.         /** 
  87.          *  输出:{1:"No.1",2:"No.2",3:[{"id":2,"name":"user1"},{"id":3,"name":"user2"}]} 
  88.          */  
  89.     }  
  90.   
  91.     /** 
  92.      * 由一个复杂的Object到Json的Demo 
  93.      */  
  94.     private static void complex2JsonM8() {  
  95.         Group group = new Group();  
  96.         group.setId(1);  
  97.         group.setName("group");  
  98.           
  99.         User user1 = new User();  
  100.         user1.setId(2);  
  101.         user1.setName("user1");  
  102.   
  103.         User user2 = new User();  
  104.         user2.setId(3);  
  105.         user2.setName("user2");  
  106.         group.getList().add(user1);  
  107.         group.getList().add(user2);  
  108.         String jsonString = JSON.toJSONString(group);  
  109.         System.out.println(jsonString);  
  110.         /** 
  111.          *  输出:{"id":1,"list":[{"id":2,"name":"user1"},{"id":3,"name":"user2"}],"name":"group"} 
  112.          */  
  113.     }  
  114.   
  115.     /** 
  116.      * 一个相对复杂的例子: 
  117.      * JSON文本数据: 
  118.      *      首先是JSONObject,转换为JSONArray; 
  119.      *      然后将JSONArray转换为JavaBean 
  120.      */  
  121.     private static void complexExampleM7() {  
  122.         String s = "{js:[{id:\"110000\",\"city\":\"北#001京市\"},{id:\"110000\",\"city\":\"北#002京市\"}"  
  123.                 + ",{id:\"110000\",\"city\":\"北#002京市\"},{id:\"110000\",\"city\":\"北#002京市\"},"  
  124.                 + "{id:\"110000\",\"city\":\"#006北#005京市\"},"  
  125.                 + "{id:\"110000\",\"city\":\"北#002京市\"},"  
  126.                 + "{id:\"110000\",\"city\":\"北#002京市\"},{id:\"120000\",\"city\":\"天#009津市\"}]}";  
  127.           
  128.         JSONObject object = JSON.parseObject(s);  
  129.         Object jsonArray = object.get("js");  
  130.         System.out.println(jsonArray);  
  131.         List<Weibo> list = JSON.parseArray(jsonArray+"", Weibo.class);  
  132.         for (Weibo weibo : list) {  
  133.             System.out.println(weibo.getCity());  
  134.         }  
  135.     }  
  136.   
  137.     /** 
  138.      * 将JavaBean转换为JSON对象 
  139.      */  
  140.     private static void bean2JSONObjectM6() {  
  141.         Weibo weibo = new Weibo("0373", "洛阳");  
  142.         JSONObject json = (JSONObject) JSON.toJSON(weibo);  
  143.         System.out.println(json.get("id"));  
  144.         /** 
  145.          * 打印: 
  146.          * 0373 
  147.          */  
  148.     }  
  149.   
  150.     /** 
  151.      * 将JSON文本转换为JavaBean的集合; 
  152.      * 内部实现肯定是:首先转换为JSONArray,然后再转换为List集合 
  153.      */  
  154.     private static void json2JavaBeanM4() {  
  155.         String s = "[{\"id\":\"0375\",\"city\":\"平顶山\"},{\"id\":\"0377\",\"city\":\"南阳\"}]";  
  156.         List<Weibo> list = JSON.parseArray(s, Weibo.class);  
  157.         for (Weibo weibo : list) {  
  158.             System.out.println(weibo.getCity());  
  159.         }  
  160.         /** 
  161.          * 打印: 
  162.          * 平顶山 
  163.          * 南阳 
  164.          */  
  165.     }  
  166.   
  167.     /** 
  168.      * 将json数据转化为JSONArray: 
  169.      * 注意:获取到JSONArray之后(我们这里只是获取到JSONArray而不是JavaBean的List集合) 
  170.      * 获取JSONArray中的数据转换为String类型需要在外边加""; 
  171.      */  
  172.     private static void json2JSONArrayM3() {  
  173.         String s = "[{\"id\":\"0375\",\"city\":\"平顶山\"},{\"id\":\"0377\",\"city\":\"南阳\"}]";  
  174.         //将JSON文本转换为JSONArray  
  175.         JSONArray array = JSON.parseArray(s);  
  176.         //这行必须写:必须加上+"";不然会报出类型强转异常!  
  177.         String str = array.get(1)+"";  
  178.         System.out.println(array.get(0));  
  179.         JSONObject object = JSON.parseObject(str);  
  180.         System.out.println(object.get("id"));  
  181.         /** 
  182.          * {"city":"平顶山","id":"0375"} 
  183.             0377 
  184.          */  
  185.     }  
  186.   
  187.   
  188.     /** 
  189.      * 将JavaBean转换为Json格式的数据/Json文本 
  190.      * 用户数据的传递:例如上传服务器 
  191.      */  
  192.     private static void bean2JsonM5() {  
  193.         Weibo weibo = new Weibo("123456", "上海");  
  194.         String string = JSON.toJSONString(weibo);  
  195.         System.out.println(string);  
  196.         /** 
  197.          * 打印: 
  198.          * {"city":"上海","id":"123456"} 
  199.          */  
  200.     }  
  201.   

如下:

JSON数据:

{
    "result":{
        "result":[
            {
                "countlist":[

                ],
                "list":[
                    {
                        "serviceFee":"0.00",
                        "mchId":"1493912972",
                        "equipment_code":"",
                        "bankType":"CFT",
                        "discount":"0.00",
                        "subMchId":"1556008101",
                        "feeType":"CNY",
                        "transactionId":"4200000388201909249814243211",
                        "tradeState":"SUCCESS",
                        "tradeTime":"2019-09-24 16:17:23",
                        "totalFee":"0.01",
                        "rate":"0.60",
                        "outTradeNo":"20190924161701",
                        "goodsName":"测试商品1",
                        "tradeType":"FACEPAY"
                    },
                    {
                        "serviceFee":"0.00",
                        "mchId":"1493912972",
                        "equipment_code":"",
                        "bankType":"CFT",
                        "discount":"0.00",
                        "subMchId":"1556008101",
                        "feeType":"CNY",
                        "transactionId":"4200000377201909248262354118",
                        "tradeState":"SUCCESS",
                        "tradeTime":"2019-09-24 15:41:25",
                        "totalFee":"0.01",
                        "rate":"0.60",
                        "outTradeNo":"20190924154110",
                        "goodsName":"测试商品1",
                        "tradeType":"FACEPAY"
                    },
                    {
                        "serviceFee":"0.00",
                        "mchId":"1493912972",
                        "equipment_code":"",
                        "bankType":"CFT",
                        "discount":"0.00",
                        "subMchId":"1556008101",
                        "feeType":"CNY",
                        "transactionId":"4200000396201909248111329354",
                        "tradeState":"SUCCESS",
                        "tradeTime":"2019-09-24 16:18:59",
                        "totalFee":"0.01",
                        "rate":"0.60",
                        "outTradeNo":"20190924161846",
                        "goodsName":"测试商品1",
                        "tradeType":"FACEPAY"
                    },
                    {
                        "serviceFee":"0.00",
                        "mchId":"1493912972",
                        "equipment_code":"",
                        "bankType":"CFT",
                        "discount":"0.00",
                        "subMchId":"1556008101",
                        "feeType":"CNY",
                        "transactionId":"4200000392201909249461361494",
                        "tradeState":"SUCCESS",
                        "tradeTime":"2019-09-24 15:36:41",
                        "totalFee":"0.01",
                        "rate":"0.60",
                        "outTradeNo":"20190924153625",
                        "goodsName":"测试商品1",
                        "tradeType":"FACEPAY"
                    },
                    {
                        "serviceFee":"0.00",
                        "mchId":"1493912972",
                        "equipment_code":"",
                        "bankType":"QDCCB_DEBIT",
                        "discount":"0.00",
                        "subMchId":"1556008101",
                        "feeType":"CNY",
                        "transactionId":"4200000386201909245289806801",
                        "tradeState":"SUCCESS",
                        "tradeTime":"2019-09-24 09:05:55",
                        "totalFee":"0.01",
                        "rate":"0.60",
                        "outTradeNo":"20190924090524",
                        "goodsName":"测试商品1",
                        "tradeType":"FACEPAY"
                    },
                    {
                        "serviceFee":"0.00",
                        "mchId":"1493912972",
                        "equipment_code":"",
                        "bankType":"CFT",
                        "discount":"0.00",
                        "subMchId":"1556008101",
                        "feeType":"CNY",
                        "transactionId":"4200000388201909249814243211",
                        "tradeState":"REFUND",
                        "tradeTime":"2019-09-24 16:27:29",
                        "totalFee":"0.00",
                        "rate":"0.60",
                        "outTradeNo":"20190924161701",
                        "goodsName":"测试商品1",
                        "tradeType":"FACEPAY"
                    },
                    {
                        "serviceFee":"0.00",
                        "mchId":"1493912972",
                        "equipment_code":"",
                        "bankType":"QDCCB_DEBIT",
                        "discount":"0.00",
                        "subMchId":"1556008101",
                        "feeType":"CNY",
                        "transactionId":"4200000373201909230181672212",
                        "tradeState":"REFUND",
                        "tradeTime":"2019-09-24 15:09:03",
                        "totalFee":"0.00",
                        "rate":"0.60",
                        "outTradeNo":"20190923172817",
                        "goodsName":"测试商品1",
                        "tradeType":"FACEPAY"
                    },
                    {
                        "serviceFee":"0.00",
                        "mchId":"1493912972",
                        "equipment_code":"",
                        "bankType":"CFT",
                        "discount":"0.00",
                        "subMchId":"1556008101",
                        "feeType":"CNY",
                        "transactionId":"4200000377201909248262354118",
                        "tradeState":"REFUND",
                        "tradeTime":"2019-09-24 17:15:43",
                        "totalFee":"0.00",
                        "rate":"0.60",
                        "outTradeNo":"20190924154110",
                        "goodsName":"测试商品1",
                        "tradeType":"FACEPAY"
                    },
                    {
                        "serviceFee":"0.00",
                        "mchId":"1493912972",
                        "equipment_code":"",
                        "bankType":"CFT",
                        "discount":"0.00",
                        "subMchId":"1556008101",
                        "feeType":"CNY",
                        "transactionId":"4200000396201909248111329354",
                        "tradeState":"REFUND",
                        "tradeTime":"2019-09-24 16:31:29",
                        "totalFee":"0.00",
                        "rate":"0.60",
                        "outTradeNo":"20190924161846",
                        "goodsName":"测试商品1",
                        "tradeType":"FACEPAY"
                    }
                ]
            }
        ],
        "total":9,
        "pageSize":0,
        "pageNum":0
    },
    "success":true,
    "error":""
}

解析:

 //阿里巴巴 JSON字符串转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(a);
        System.err.println("阿里巴巴 JSON字符串转JSONObject对象"+jsonObject);
        //获取到 属性"result" 转换成对象
        JSONObject jsonObject1 = JSON.parseObject(jsonObject.get("result").toString());
        System.err.println("获取到 属性\"result\" 转换成对象"+jsonObject1);
        // 获取到 属性"result" 转换成数组
        JSONArray parseArray = JSON.parseArray(jsonObject1.get("result").toString());
        System.err.println("获取到 属性\"result\" 转换成数组"+parseArray);
        // 数组转对象
        JSONObject jsonObject2 = JSON.parseObject(parseArray.get(0).toString());
        System.err.println("数组转对象"+jsonObject2);
        //获取到 "list" 转换成 数组
        JSONArray jsonArray = JSON.parseArray(jsonObject2.get("list").toString());
        System.err.println("获取到 \"list\" 转换成 数组"+jsonArray);
        //遍历数组
        for (int i = 0; i <jsonArray.size() ; i++) {
            //找到具体的值
            System.err.println(jsonArray.getJSONObject(i).get("mchId"));
        }

打印结果:

阿里巴巴 JSON字符串转JSONObject对象:{"result":{"result":[{"countlist":[],"list":[{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000388201909249814243211","tradeState":"SUCCESS","tradeTime":"2019-09-24 16:17:23","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924161701","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000377201909248262354118","tradeState":"SUCCESS","tradeTime":"2019-09-24 15:41:25","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924154110","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000396201909248111329354","tradeState":"SUCCESS","tradeTime":"2019-09-24 16:18:59","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924161846","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000392201909249461361494","tradeState":"SUCCESS","tradeTime":"2019-09-24 15:36:41","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924153625","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"QDCCB_DEBIT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000386201909245289806801","tradeState":"SUCCESS","tradeTime":"2019-09-24 09:05:55","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924090524","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000388201909249814243211","tradeState":"REFUND","tradeTime":"2019-09-24 16:27:29","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924161701","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"QDCCB_DEBIT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000373201909230181672212","tradeState":"REFUND","tradeTime":"2019-09-24 15:09:03","totalFee":"0.00","rate":"0.60","outTradeNo":"20190923172817","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000377201909248262354118","tradeState":"REFUND","tradeTime":"2019-09-24 17:15:43","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924154110","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000396201909248111329354","tradeState":"REFUND","tradeTime":"2019-09-24 16:31:29","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924161846","goodsName":"测试商品1","tradeType":"FACEPAY"}]}],"total":9,"pageSize":0,"pageNum":0},"success":true,"error":""}
获取到 属性"result" 转换成对象:{"result":[{"countlist":[],"list":[{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000388201909249814243211","tradeState":"SUCCESS","tradeTime":"2019-09-24 16:17:23","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924161701","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000377201909248262354118","tradeState":"SUCCESS","tradeTime":"2019-09-24 15:41:25","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924154110","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000396201909248111329354","tradeState":"SUCCESS","tradeTime":"2019-09-24 16:18:59","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924161846","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000392201909249461361494","tradeState":"SUCCESS","tradeTime":"2019-09-24 15:36:41","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924153625","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"QDCCB_DEBIT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000386201909245289806801","tradeState":"SUCCESS","tradeTime":"2019-09-24 09:05:55","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924090524","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000388201909249814243211","tradeState":"REFUND","tradeTime":"2019-09-24 16:27:29","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924161701","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"QDCCB_DEBIT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000373201909230181672212","tradeState":"REFUND","tradeTime":"2019-09-24 15:09:03","totalFee":"0.00","rate":"0.60","outTradeNo":"20190923172817","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000377201909248262354118","tradeState":"REFUND","tradeTime":"2019-09-24 17:15:43","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924154110","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000396201909248111329354","tradeState":"REFUND","tradeTime":"2019-09-24 16:31:29","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924161846","goodsName":"测试商品1","tradeType":"FACEPAY"}]}],"total":9,"pageSize":0,"pageNum":0}
获取到 属性"result" 转换成数组:[{"countlist":[],"list":[{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000388201909249814243211","tradeState":"SUCCESS","tradeTime":"2019-09-24 16:17:23","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924161701","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000377201909248262354118","tradeState":"SUCCESS","tradeTime":"2019-09-24 15:41:25","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924154110","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000396201909248111329354","tradeState":"SUCCESS","tradeTime":"2019-09-24 16:18:59","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924161846","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000392201909249461361494","tradeState":"SUCCESS","tradeTime":"2019-09-24 15:36:41","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924153625","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"QDCCB_DEBIT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000386201909245289806801","tradeState":"SUCCESS","tradeTime":"2019-09-24 09:05:55","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924090524","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000388201909249814243211","tradeState":"REFUND","tradeTime":"2019-09-24 16:27:29","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924161701","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"QDCCB_DEBIT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000373201909230181672212","tradeState":"REFUND","tradeTime":"2019-09-24 15:09:03","totalFee":"0.00","rate":"0.60","outTradeNo":"20190923172817","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000377201909248262354118","tradeState":"REFUND","tradeTime":"2019-09-24 17:15:43","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924154110","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000396201909248111329354","tradeState":"REFUND","tradeTime":"2019-09-24 16:31:29","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924161846","goodsName":"测试商品1","tradeType":"FACEPAY"}]}]
数组转对象:{"countlist":[],"list":[{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000388201909249814243211","tradeState":"SUCCESS","tradeTime":"2019-09-24 16:17:23","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924161701","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000377201909248262354118","tradeState":"SUCCESS","tradeTime":"2019-09-24 15:41:25","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924154110","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000396201909248111329354","tradeState":"SUCCESS","tradeTime":"2019-09-24 16:18:59","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924161846","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000392201909249461361494","tradeState":"SUCCESS","tradeTime":"2019-09-24 15:36:41","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924153625","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"QDCCB_DEBIT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000386201909245289806801","tradeState":"SUCCESS","tradeTime":"2019-09-24 09:05:55","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924090524","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000388201909249814243211","tradeState":"REFUND","tradeTime":"2019-09-24 16:27:29","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924161701","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"QDCCB_DEBIT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000373201909230181672212","tradeState":"REFUND","tradeTime":"2019-09-24 15:09:03","totalFee":"0.00","rate":"0.60","outTradeNo":"20190923172817","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000377201909248262354118","tradeState":"REFUND","tradeTime":"2019-09-24 17:15:43","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924154110","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000396201909248111329354","tradeState":"REFUND","tradeTime":"2019-09-24 16:31:29","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924161846","goodsName":"测试商品1","tradeType":"FACEPAY"}]}
获取到 "list" 转换成 数组:[{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000388201909249814243211","tradeState":"SUCCESS","tradeTime":"2019-09-24 16:17:23","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924161701","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000377201909248262354118","tradeState":"SUCCESS","tradeTime":"2019-09-24 15:41:25","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924154110","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000396201909248111329354","tradeState":"SUCCESS","tradeTime":"2019-09-24 16:18:59","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924161846","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000392201909249461361494","tradeState":"SUCCESS","tradeTime":"2019-09-24 15:36:41","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924153625","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"QDCCB_DEBIT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000386201909245289806801","tradeState":"SUCCESS","tradeTime":"2019-09-24 09:05:55","totalFee":"0.01","rate":"0.60","outTradeNo":"20190924090524","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000388201909249814243211","tradeState":"REFUND","tradeTime":"2019-09-24 16:27:29","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924161701","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"QDCCB_DEBIT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000373201909230181672212","tradeState":"REFUND","tradeTime":"2019-09-24 15:09:03","totalFee":"0.00","rate":"0.60","outTradeNo":"20190923172817","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000377201909248262354118","tradeState":"REFUND","tradeTime":"2019-09-24 17:15:43","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924154110","goodsName":"测试商品1","tradeType":"FACEPAY"},{"serviceFee":"0.00","mchId":"1493912972","equipment_code":"","bankType":"CFT","discount":"0.00","subMchId":"1556008101","feeType":"CNY","transactionId":"4200000396201909248111329354","tradeState":"REFUND","tradeTime":"2019-09-24 16:31:29","totalFee":"0.00","rate":"0.60","outTradeNo":"20190924161846","goodsName":"测试商品1","tradeType":"FACEPAY"}]
1493912972
1493912972
1493912972
1493912972
1493912972
1493912972
1493912972
1493912972
1493912972

------------------------------------------------------------

{
"data":{
"landing_page_id":10000,
"url":"https://pages.xiaohongshu.com/ad/effect/test",
 "submitted_time":"2019-10-15 13:19:45",
 "source_channel":"信息流",
 "account_id":"5afbb4c54eacab482919dcb2",
 "account_name":"薯管家",
 "campaign_id":10000,
 "campaign_name":"测试计划",
 "unit_id":10000,
 "unit_name":"测试单元",
 "creative_id":10000,
 "leads_id":"5da556f1ef7569c2434cd3b8",
 "data":[
 {
 "label":"姓名",

 "value":"薯管家"
 },
 {
 "label":"手机号",
 "value":"123xxxxxxxx"
 },
 {
 "label":"邮箱",
 "value":"test@xiaohongshu.com"
 },
 {
 "label":"省份",
 "value":"上海"
 },
 {
 "label":"城市",
 "value":"上海市"
 },
 {
 "label":"详细地址",
 "value":"xxx"
 },
 {
 "label":"性别",
 "value":"男"
 },
 {
 "label":"数值",
 "value":"515"
 },
 {
 "label":"多选",
 "value":"[\"选项 2\",\"选项 1\",\"选项 3\"]"
 },
 {
 "label":"文本",
 "value":"123abc"
 },
 {
 "label":"单选",
 "value":"选项 3"
 }
 ]
 },
 "timestamp":1571116785851,
 "source":"小红书"
 }

 

 

JSONObject parseObject = JSON.parseObject(json);
System.err.println(parseObject);
JSONObject data = parseObject.getJSONObject("data");
System.err.println(data);
JSONArray data1 = data.getJSONArray("data");
System.err.println(data1);
for (int i = 0; i < data1.size(); i++) {
    System.out.println(data1.getJSONObject(i).get("label"));
}