<Json字符串解析>Json转List<Map<String,Object>>案例一

Json数据举例如:

	"data":[
		{
			"task":"startProduction",
			"form":"汽车生产数据的枚举值映射关系",
			"startTime":"2022-02-26",
			"endTime":"2022-03-01",
			"formValue":{
				"car_1":"小汽车",
				"color_1":"red",
				"brand_1":"Audi",
				"modelNumber_1":"A6",
				"price_1":"360000",
				"creatValue":{
					"creatBy":"奥斯莱卡特",
					"age":"35"
				},
				"DateProduction_1":"2022-03-08"
			},
			"taskId":"aaa123"
		},
		{
			"task":"endProduction",
			"form":"汽车生产数据的枚举值映射关系",
			"startTime":"2022-02-26",
			"endTime":"2022-03-01",
			"formValue":{
				"car_2":"小汽车",
				"color_2":"while",
				"brand_2":"BMW",
				"modelNumber_2":"X5",
				"price_2":"420000",
				"DateProduction_2":"2022-03-08"
			},
			"taskId":"aaa123456"
		}
	],
	"code":200,
	"message":"success"
}

解析代码如下:

package com.itheima;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonUtilCar {
    public static void main(String[] args) {
        String jsonData = "";
        List<Map<String,Object>> list = getJsonData(jsonData);
        System.out.println("list:"+list.size());
        for (Map<String,Object> map:list) {
            System.out.println(map);
        }
    }

    public static List<Map<String,Object>> getJsonData(String jsonData){
		//创建集合对象
        List<Map<String, Object>> list = new ArrayList();
		//转成jsonObject对象,返回:JSONObject 
        JSONObject jsonObject = JSON.parseObject(jsonData);
        //获取data数据,返回:JSONArray 
        JSONArray data = jsonObject.getJSONArray("data");

        if(data.size()>0){
            for(int i=0;i<data.size();i++){
				//创建Map对象
                Map<String, Object> map = new HashMap<>();
				
                //公共参数统一解析
                //获取data对象里面的每一组数据,返回:JSONObject
                JSONObject job = data.getJSONObject(i);
                String task = String.valueOf(job.get("task"));
                String startTime = String.valueOf(job.get("startTime"));
                String endTime = String.valueOf(job.get("endTime"));
                String taskId = String.valueOf(job.get("taskId"));
                map.put("task",task);
                map.put("startTime",startTime);
                map.put("endTime",endTime);
                map.put("taskId",taskId);
				
				//不同参数分开解析
                //奥迪生产流程工艺
                if(task.equals("startProduction")){
                	//获取formValue数据,返回:JSONObject 
                    JSONObject formValue = job.getJSONObject("formValue");
                    String car = String.valueOf(formValue.get("car_1"));
                    String color = String.valueOf(formValue.get("color_1"));
                    String brand = String.valueOf(formValue.get("brand_1"));
                    String modelNumber = String.valueOf(formValue.get("modelNumber_1"));
                    String price = String.valueOf(formValue.get("price_1"));
					String DateProduction = String.valueOf(formValue.get("DateProduction_1"));
                    //获取:creatValue对象数据
                    JSONObject creatValue = formValue.getJSONObject("creatValue");
                    String creatBy = String.valueOf(creatValue.get("creatBy"));
                    String age = String.valueOf(creatValue.get("age"));
                    map.put("creatBy",creatBy);
                    map.put("age",age);
					
                    map.put("car",car);
                    map.put("color",color);
                    map.put("brand",brand);
                    map.put("modelNumber",modelNumber);
                    map.put("price",price);
					map.put("DateProduction",DateProduction);
                }
                //宝马生产流程工艺
                if(task.equals("endProduction")){
                	//获取formValue数据,返回:JSONObject 
                    JSONObject formValue = job.getJSONObject("formValue");
                    String car = String.valueOf(formValue.get("car_2"));
                    String color = String.valueOf(formValue.get("color_2"));
                    String brand = String.valueOf(formValue.get("brand_2"));
                    String modelNumber = String.valueOf(formValue.get("modelNumber_2"));
                    String price = String.valueOf(formValue.get("price_2"));
					String DateProduction = String.valueOf(formValue.get("DateProduction_2"));
					
                    map.put("car",car);
                    map.put("color",color);
                    map.put("brand",brand);
                    map.put("modelNumber",modelNumber);
                    map.put("price",price);
					map.put("DateProduction",DateProduction);
                }
                //添加集合
                list.add(map);
            }
        }
        return list;
    }
}

你可能感兴趣的:(ListMap,Json,java,json,list,java)