java实现将Json数组字符串转换成List列表

Json 数组字符串内容如下

        String res = "[\n" +
                "    {\n" +
                "        \"artifact_count\": 3,\n" +
                "        \"creation_time\": \"2022-04-22T08:13:41.746Z\",\n" +
                "        \"id\": 30,\n" +
                "        \"name\": \"base/statistic-service-base\",\n" +
                "        \"project_id\": 2,\n" +
                "        \"pull_count\": 0,\n" +
                "        \"update_time\": \"2022-04-22T08:13:41.746Z\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"artifact_count\": 1,\n" +
                "        \"creation_time\": \"2022-04-22T08:12:19.763Z\",\n" +
                "        \"id\": 29,\n" +
                "        \"name\": \"base/tomcat_ra\",\n" +
                "        \"project_id\": 2,\n" +
                "        \"pull_count\": 0,\n" +
                "        \"update_time\": \"2022-04-22T08:12:19.763Z\"\n" +
                "    }]";

转换成 List列表

1、按 json 数组的字段新建一个实体类

import lombok.Data;
import java.io.Serializable;

/**
 * @author yuhuofei2021
 * @version 1.0
 * @date 2022/6/8 13:37
 */
@Data
public class ImageInfoVO implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer artifactCount;

    private String creationTime;

    private Integer id;

    private String name;
    
    private Integer projectId;

    private Integer pullCount;

    private String updateTime;
}

2、转换成 List 列表

  • 引入下面的 jar 包
import com.alibaba.fastjson.JSONArray;
  • 调用转换方法
 List<ImageInfoVO> jsonList = JSONArray.parseArray(res, ImageInfoVO.class);

你可能感兴趣的:(Java,java,json)