jackson: 字符串和对象的相互转换

JSON字符串:

 

{

    "flinkJobConfigList": [

        {

            "displayGroupName": "基础",

            "displayGroupList": [

                {

                    "displayName": "类加载模式",

                    "confName": "classloader.resolve-order",

                    "confDefaultValue": "parent-first",

                    "jobConfigurationDesc": "配置类加载模式"

                },

                {

                    "displayName": "JM内存",

                    "confName": "parallelism.default",

                    "confDefaultValue": "1",

                    "jobConfigurationDesc": "作业并行度"

                },

                {

                    "displayName": "JM内存",

                    "confName": "jobmanager.memory.process.size",

                    "confDefaultValue": "1024",

                    "jobConfigurationDesc": "作业内存"

                },

                {

                    "displayName": "TM内存",

                    "confName": "taskmanager.memory.process.size",

                    "confDefaultValue": "600",

                    "jobConfigurationDesc": "作业TM内存"

                },

                {

                    "displayName": "TM slot数",

                    "confName": "taskmanager.numberOfTaskSlots",

                    "confDefaultValue": "6",

                    "jobConfigurationDesc": ""

                }

            ]

        },

        {

            "displayGroupName": "状态后端及检查点",

            "displayGroupList": [

                {

                    "displayName": "是否开启检查点",

                    "confName": "checkpoint",

                    "confDefaultValue": "true",

                    "jobConfigurationDesc": "检查点"

                },

                {

                    "displayName": "频率",

                    "confName": "execution.checkpointing.interval",

                    "confDefaultValue": "5",

                    "jobConfigurationDesc": ""

                },

                {

                    "displayName": "状态后端类型",

                    "confName": "state.backend",

                    "confDefaultValue": "rocksdb",

                    "jobConfigurationDesc": ""

                }

            ]

        }

    ]

}

 

 

实体对象

public class DisplayGroup {

	//展示名
	private String displayName;
    //配置项名
	private String confName;
    //配置项默认值
	private String confDefaultValue;
    //描述
	private String jobConfigurationDesc;
	
	public String getDisplayName() {
		return displayName;
	}
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
	public String getConfName() {
		return confName;
	}
	public void setConfName(String confName) {
		this.confName = confName;
	}
	public String getConfDefaultValue() {
		return confDefaultValue;
	}
	public void setConfDefaultValue(String confDefaultValue) {
		this.confDefaultValue = confDefaultValue;
	}
	public String getJobConfigurationDesc() {
		return jobConfigurationDesc;
	}
	public void setJobConfigurationDesc(String jobConfigurationDesc) {
		this.jobConfigurationDesc = jobConfigurationDesc;
	}
	
}
import java.util.Collections;
import java.util.List;

public class FlinkJobConfiguration {
	//展示分组
	private String displayGroupName;
	
	private List<DisplayGroup> displayGroupList = Collections.emptyList();

	public String getDisplayGroupName() {
		return displayGroupName;
	}

	public void setDisplayGroupName(String displayGroupName) {
		this.displayGroupName = displayGroupName;
	}

	public List<DisplayGroup> getDisplayGroupList() {
		return displayGroupList;
	}

	public void setDisplayGroupList(List<DisplayGroup> displayGroupList) {
		this.displayGroupList = displayGroupList;
	}

}
import java.util.ArrayList;
import java.util.List;

public class FlinkJobConfigList {

	private List<FlinkJobConfiguration> flinkJobConfigList = new ArrayList<>();

	public List<FlinkJobConfiguration> getFlinkJobConfigList() {
		return flinkJobConfigList;
	}

	public void setFlinkJobConfigList(List<FlinkJobConfiguration> flinkJobConfigList) {
		this.flinkJobConfigList = flinkJobConfigList;
	}

}

@RestController
@RequestMapping("test")
public class TestRest {

@PostMapping("/jsonToObject")
    public ResultBean jsonToObject(@RequestBody FlinkJobConfigList flinkJobConfigList) {
        FlinkJobConfigList flinkJobConfigListNew = new FlinkJobConfigList();
        flinkJobConfigListNew = flinkJobConfigList;
        return success(flinkJobConfigListNew);
    }

}

 

测试类-Json字符串转对象

import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.midea.bigdata.datamax.flink.model.FlinkJobConfigList;  
  


public class JsonUtil {
	
	public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
		
//		String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"xiaomin@sina.com\"}";
		String json = "{\"flinkJobConfigList\":[{\"displayGroupName\":\"基础\",\"displayGroupList\":[{\"displayName\":\"类加载模式\",\"confName\":\"classloader.resolve-order\",\"confDefaultValue\":\"parent-first\",\"jobConfigurationDesc\":\"配置类加载模式\"},{\"displayName\":\"JM内存\",\"confName\":\"parallelism.default\",\"confDefaultValue\":\"1\",\"jobConfigurationDesc\":\"作业并行度\"},{\"displayName\":\"JM内存\",\"confName\":\"jobmanager.memory.process.size\",\"confDefaultValue\":\"1024\",\"jobConfigurationDesc\":\"作业内存\"},{\"displayName\":\"TM内存\",\"confName\":\"taskmanager.memory.process.size\",\"confDefaultValue\":\"600\",\"jobConfigurationDesc\":\"作业TM内存\"},{\"displayName\":\"TM slot数\",\"confName\":\"taskmanager.numberOfTaskSlots\",\"confDefaultValue\":\"6\",\"jobConfigurationDesc\":\"\"}]},{\"displayGroupName\":\"状态后端及检查点\",\"displayGroupList\":[{\"displayName\":\"是否开启检查点\",\"confName\":\"checkpoint\",\"confDefaultValue\":\"true\",\"jobConfigurationDesc\":\"检查点\"},{\"displayName\":\"频率\",\"confName\":\"execution.checkpointing.interval\",\"confDefaultValue\":\"5\",\"jobConfigurationDesc\":\"\"},{\"displayName\":\"状态后端类型\",\"confName\":\"state.backend\",\"confDefaultValue\":\"rocksdb\",\"jobConfigurationDesc\":\"\"}]}]}";
		/**
		 * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
		 */
		ObjectMapper mapper = new ObjectMapper();
		FlinkJobConfigList user = mapper.readValue(json, FlinkJobConfigList.class);
		System.out.println(user);
	}
	
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,JSON数据可以用字符串表示,而在Java中,我们经常使用List来表示一个具有多个元素的集合。在实际的开发中,经常需要将JSON字符串转换为Java中的List对象,或者将一个List对象转换为JSON字符串。 对于将JSON字符串转换为Java中的List对象的情况,可以使用JSON解析库,如Jackson、Gson等。这些库提供了将JSON字符串转换为Java对象的方法。以Gson为例,可以使用以下代码将JSON字符串转换为List对象: ``` String jsonString = "[{"name": "张三", "age": 20}, {"name": "李四", "age": 22}]"; Gson gson = new Gson(); Type listType = new TypeToken<List<Person>>(){}.getType(); // Person是自定义的类 List<Person> personList = gson.fromJson(jsonString, listType); ``` 在将Java中的List对象转换为JSON字符串时,也可以使用相应的JSON转换库。以Jackson为例,可以使用以下代码将List对象转换为JSON字符串: ``` List<Person> personList = new ArrayList<>(); personList.add(new Person("张三", 20)); personList.add(new Person("李四", 22)); ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writeValueAsString(personList); ``` 需要注意的是,在将Java对象转换为JSON字符串时,Java对象必须满足一定的规范,例如必须包含默认的无参构造函数、成员变量必须有对应的getter和setter方法等。同时,JSON中的key与Java对象的成员变量名需要完全一致。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值