jsonArray jsonString list 之间转换

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 转换
 * @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 students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        System.out.println("List为:"+ students);

        //fastjson  List => JSONArray
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(students));
        System.out.println("fastjson  List 转 JSONArray:" + jsonArray);
        //jsonString
        String jsonString = jsonArray.toJSONString();
          // String jsonString = JSONArray.toJSONString(students);
        System.out.println("JSONString 为:" + jsonString);
        //fastJSon  JSONArray =>List
        List list = JSONObject.parseArray(jsonArray.toJSONString(),Student.class);
        System.out.println("fastjson  JSONArray 转 List:" + list);
        //jsonString 字符串 转 list
        List list1 = JSONObject.parseArray(jsonString,Student.class);
        System.out.println("fastjson  jsonString 转 List:" + list1);
     }
}
 
  
 

  2、结果jsonArray jsonString list<Object> 之间转换_第1张图片

 

 

 

你可能感兴趣的:(jsonArray jsonString list 之间转换)