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、结果

  • 相关阅读:
    centos7配置java环境
    puppet使用 apache passsenger 作为前端 (debian)
    puppet 和 apache passenger的配置
    puppet 安装
    JQuery Plugin 开发
    Shell脚本中的 测试开关 和 特殊参数
    CPPUTest 单元测试框架(针对 C 单元测试的使用说明)
    Makefile 使用总结
    挂载KVM Guest操作系统磁盘
    Linux资源管理-IO优先级
  • 原文地址:https://www.cnblogs.com/godpo/p/11993847.html
Copyright © 2011-2022 走看看