zoukankan      html  css  js  c++  java
  • jsonArray jsonString list<Object> 之间转换

    1、示例:

    package com.test.demo.pojo;
    
    import lombok.Data;
    import lombok.experimental.Accessors;
    
    /**
     * @program: demo
     * @description:
     * @author: ZhuGaoPo
     * @version:1.0
     * @create: 2019-12-06 09:51
     */
    @Data
    @Accessors(chain = true)
    public class Student {
        private String name;
        private String gender;
        private int age;
    }
    

      

    package com.test.demo.test;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import com.test.demo.pojo.Student;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @program: demo
     * @description: jsonString jsonArray jsonObject List<Object> 转换
     * @copyRight:Sunyard
     * @author: ZhuGaoPo
     * @version:1.0
     * @create: 2019-12-06 09:49
     */
    public class JsonTest {
        public static void main(String[] args) {
            Student s1 = new Student();
            Student s2 = new Student();
            Student s3 = new Student();
            s1.setName("小明").setAge(18).setGender("男");
            s2.setName("小红").setAge(16).setGender("女");
            s3.setName("小刚").setAge(19).setGender("男");
            List<Student> students = new ArrayList<>();
            students.add(s1);
            students.add(s2);
            students.add(s3);
            System.out.println("List<Student>为:"+ students);
    
            //fastjson  List<Object> => JSONArray
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(students));
            System.out.println("fastjson  List<Object> 转 JSONArray:" + jsonArray);
            //jsonString
            String jsonString = jsonArray.toJSONString();
              // String jsonString = JSONArray.toJSONString(students);
            System.out.println("JSONString 为:" + jsonString);
            //fastJSon  JSONArray =>List<Object>
            List<Student> list = JSONObject.parseArray(jsonArray.toJSONString(),Student.class);
            System.out.println("fastjson  JSONArray 转 List<Object>:" + list);
            //jsonString 字符串 转 list
            List<Student> list1 = JSONObject.parseArray(jsonString,Student.class);
            System.out.println("fastjson  jsonString 转 List<Object>:" + list1);
         }
    }
    

      2、结果

  • 相关阅读:
    vuejs中使用echart图表
    锚点链接
    如何动态修改网页的标题(title)?
    如何为图片添加热点链接?(map + area)
    cookie
    如何为你的网站添加标志性的图标(头像)呢?
    图片拖拽上传至服务器
    js定时器之setTimeout的使用
    input[type=file]中使用ajaxSubmit来图片上传
    input[type=file]样式更改以及图片上传预览
  • 原文地址:https://www.cnblogs.com/godpo/p/11993847.html
Copyright © 2011-2022 走看看