• JSON中toJSONString、ParseObject、parseArray的作用以及用 com.alibaba.fast.JSONArray解析字符串或者List集合


     if(scenicInfo.getKindsImage() != null){
    List<String> imageUrl = JSONArray.parseArray(scenicInfo.getKindsImage(),String.class);
    scenicInfo.setKindsImage(imageUrl.get(0));
    }

    (1)parseArray的作用

    import com.alibaba.fastjson.JSON;
    import lombok.Data;
    import org.junit.Test;

    import java.util.List;

    public class JSONTest {
        @Test
        public void JSONTest() {
            //从Redis中查出来的字符串
            String str = "[{"ct_pt":"xxx","data_time":"xxx","mp_id":" +
                    ""xxx","mp_name":"xxx","pos_p_e_total_e":"xxx","" +
                    "pos_p_e_total_s":"xxx","pos_p_e_total_sub":"xxx"," +
                    ""pos_p_e_total_subQuantity":"xxx"}]";
            //将字符串转换成list
            List<Emp> parseArray = JSON.parseArray(str, Emp.class);
            for (Emp emp : parseArray){
                System.out.println(emp);
            }
        }

    }

    @Data
    class Emp {
        private String ct_pt;
        private String data_time;
        private String mp_id;
        private String mp_name;
        private String pos_p_e_total_e;
        private String pos_p_e_total_s;
        private String pos_p_e_total_sub;
        private String pos_p_e_total_subQuantity;
    }

    输出结果:


    (2)toJSONString的作用

    package com.navict.controller;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import lombok.Data;
    import org.junit.Test;

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

    public class JSONTest {
        @Test
        public void JSONTest() {
            List<Emp> list = new ArrayList<>();

            Emp emp = new Emp();
            emp.setCt_pt("1");
            emp.setData_time(new Date());
            emp.setMp_id("1");
            emp.setMp_name("李四");
            emp.setPos_p_e_total_e("1");
            list.add(emp);
            //将集合转换成字符串
            String str = JSON.toJSONString(list);
            System.out.println(str);
        }

    }

    @Data
    class Emp {
        private String ct_pt;
        private Date data_time;
        private String mp_id;
        private String mp_name;
        private String pos_p_e_total_e;
    }

     
       

    运行结果

    (3)ParseObject的作用

    package com.navict.controller;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import lombok.Data;
    import org.junit.Test;

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

    public class JSONTest {
        @Test
        public void JSONTest() {
            String str = "{"ct_pt":"xxx","data_time":"xxx","mp_id":" +
                    ""xxx","mp_name":"xxx","pos_p_e_total_e":"xxx","" +
                    "pos_p_e_total_s":"xxx","pos_p_e_total_sub":"xxx"," +
                    ""pos_p_e_total_subQuantity":"xxx"}";
            //将字符串转换成对象
            JSONObject jsonObject = JSONObject.parseObject(str);
            String ct_pt = (String)jsonObject.get("ct_pt");
            String mp_id = (String)jsonObject.get("ct_pt");
           // Date data_time =(Date) jsonObject.get("data_time");
            String mp_name = (String)jsonObject.get("mp_name");
            String pos_p_e_total_e = (String)jsonObject.get("pos_p_e_total_e");
            Emp testEmp = new Emp();
            testEmp.setMp_id(mp_id);
            testEmp.setPos_p_e_total_e(pos_p_e_total_e);
            testEmp.setMp_name(mp_name);
            //testEmp.setData_time(data_time);
            testEmp.setCt_pt(ct_pt);
            System.out.println(JSONObject.toJSON(testEmp));
            System.out.println(jsonObject.get("ct_pt"));
        }

    }

    @Data
    class Emp {
        private String ct_pt;
        private Date data_time;
        private String mp_id;
        private String mp_name;
        private String pos_p_e_total_e;
    }

     
       


    (4) com.alibaba.fast.JSONArray的作用

    package com.navict.controller;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import lombok.Data;
    import org.junit.Test;

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

    public class JSONTest {
        @Test
        public void JSONTest() {
            String str = "{"ct_pt":"xxx","data_time":"xxx","mp_id":" +
                    ""xxx","mp_name":"xxx","pos_p_e_total_e":"xxx","" +
                    "pos_p_e_total_s":"xxx","pos_p_e_total_sub":"xxx"," +
                    ""pos_p_e_total_subQuantity":"xxx"}";
            //将字符串转换成对象
            Emp emp = JSONArray.parseObject(str, Emp.class);
            System.out.println(JSONObject.toJSONString(emp));
        }

    }

    @Data
    class Emp {
        private String ct_pt;
        private Date data_time;
        private String mp_id;
        private String mp_name;
        private String pos_p_e_total_e;
    }

       
    运行结果:

    ————————————————
    原文链接:https://blog.csdn.net/hukehukehukehuke/article/details/106198340

  • 相关阅读:
    值类型、引用类型的区别
    SharePoint Server 2013 Excel Web Access无法显示
    SharePoint 2013报错之“指定的文件不是有效的电子表格或者没有包含要导入的数据”
    SharePoint 2013备份方法整理
    SQL 2005报错之Restore fail for Server 'DatabaseServerName'.
    SQL Server 2012自动备份
    SharePoint 2013在浏览器中打开pdf文档
    摸鱼,搞RW
    蛋疼的远程声音,这次用蓝牙试试看
    Win10 IoT Core 更改密码(PowerShell)
  • 原文地址:https://www.cnblogs.com/yangsanluo/p/14911603.html
Copyright © 2020-2023  润新知