文章来源:https://www.toymoban.com/news/detail-773547.html
1.Java中对象和json互转
Object obj = new Object();
String objJson = JSONObject.toJSONString(obj);//java对象转json
Object newObj = JSONObject.parseObject(objJson, Object.class);//json转java对象
2.Java中list和json互转
List<Object> list = new ArrayList<>();
String listJson = JSONObject.toJSONString(list);//list转json
List<Object> newList = JSONObject.parseArray(listJson, Object.class);//json转list
3.Java中map和json互转
Map<String, Object> map= new HashMap<>();
String mapJson = JSONObject.parseObject(map, Map.class);//map转json
Map newMap = JSONObject.toJavaObject(JSONObject.parseObject(mapJson), Map.class);//json转map
4.定制化配置Java对象和JSON之间的转换
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class SpringBeanConfiguration {
@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { // 方法声明
ObjectMapper objectMapper = builder.createXmlMapper(false).build(); // 创建ObjectMapper对象
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); // 设置序列化时包含非空字段
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 设置序列化时包含非null字段
JavaTimeModule timeModule = new JavaTimeModule(); // 创建JavaTimeModule对象,用于处理Java 8时间类的序列化和反序列化
timeModule.addDeserializer(LocalDateTime.class, // 添加LocalDateTime的反序列化器,指定日期时间格式
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
timeModule.addSerializer(LocalDateTime.class, // 添加LocalDateTime的序列化器,指定日期时间格式
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
objectMapper.disable(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS); // 禁用重复模块注册的特性
objectMapper.registerModules(timeModule); // 注册JavaTimeModule模块
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 配置在反序列化时忽略未知属性
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); // 配置在序列化时忽略空对象
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); // 配置允许单引号
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); // 配置接受大小写不敏感的属性
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); // 配置接受空字符串作为null对象
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { // 设置空值的序列化器,将null值序列化为空字符串
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString("");
}
});
return objectMapper; // 返回配置好的ObjectMapper对象
}
}
参考文章:
Java-json相关转换,JSONObject与实体类/map互转、List/List<map>和JSONArray互转、获取JSONObject中的key value、字符串String转换等
【JSON转换】String与JSONObject、JSONArray、JAVA对象和List 的相互转换文章来源地址https://www.toymoban.com/news/detail-773547.html
到了这里,关于JAVA对象、List、Map和JSON之间的相互转换的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!