解决json字符串序列化后的顺序问题

 更新时间:2021年03月15日 14:15:17   作者:peng-peng-  
这篇文章主要介绍了解决json字符串序列化后的顺序问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

1、应用场景:

如果项目中用到json字符串转为jsonObject的需求,并且,需要保证字符串的顺序转之前和转成jsonObject之后输出的结果完全一致。可能有点绕口,下面举一个应用场景的例子。

在做项目的过程中,需要写Junit单元测试,有一个方法如下:

 @Test
 @SuppressWarnings("unchecked")
 public void facilitySoftwareQueryByPageExample() throws Exception {
  facilitySoftwareRepository.deleteAll();
  FacilitySoftwareConfig facilitySoftware = createFacilitySoftware();
  facilitySoftwareRepository.save(facilitySoftware);
  String userId = "1";
  int pageNumber = 1;
  int pageSize = 5;
  String facilities = objectMapper.writeValueAsString(facilitySoftware);
  LinkedHashMap<String, Object> jsonMap = JSON.parseObject(facilities,LinkedHashMap.class, Feature.OrderedField);
  JSONArray jsonArray = new JSONArray();
  JSONObject jsonObject = new JSONObject(true);
  jsonObject.putAll(jsonMap);
  jsonArray.add(jsonObject);
  this.mockMvc
   .perform(get("/v1/facilitysoftware/userid/" + userId + "/page/" + pageNumber
     + "/pagesize/" + pageSize + ""))
   .andExpect(status().isOk()).andExpect(jsonPath("content",is(jsonArray)))
   .andExpect(jsonPath("totalPages", is(1)))
   .andExpect(jsonPath("totalElements", is(1)))
   .andExpect(jsonPath("last", is(true)))
   .andExpect(jsonPath("number", is(0)))
   .andExpect(jsonPath("size", is(5)))
   .andExpect(jsonPath("numberOfElements", is(1)))
   .andExpect(jsonPath("first", is(true)))
   .andDo(document("facilitySoftware-query-example"));
 }

例子就在这里:

.andExpect(status().isOk()).andExpect(jsonPath("content",is(jsonArray)))

大家应该都能读懂,这行代码意思就是你用Api获取到的json字符串和你定义的字符串是否一致,一致则该条件测试通过。

这里的比较不仅仅要求所有的key和value都相同,而且需要保证两个json串的顺序完全相同,才可以完成该条件的测试。

查了资料解决途径过程如下:首先我们使用的是阿里的fastJson,需要引入fastJson的依赖,具体百度maven仓库,注意这里尽量使用稳定版本的较高版本。如 1.2.*

在解决问题过程中,遇到如下解决方案

1、在初始化json对象的时候加上参数true,这里不完全符合我们的需求,加上true之后,是让json串按照key的hashcode排序。

可以自定义升序或者降序,因为解决不了该场景的问题。这里不赘述,自行百度。

JSONObject jsonObject = new JSONObject(true);

2、解决问题,代码如下,第一个参数是需要转换的json字符串。

LinkedHashMap<String, Object> jsonMap = JSON.parseObject(facilities,LinkedHashMap.class, Feature.OrderedField);
JSONArray jsonArray = new JSONArray();
  JSONObject jsonObject = new JSONObject(true);
  jsonObject.putAll(jsonMap);
  jsonArray.add(jsonObject);

补充:JSON 序列化key排序问题和序列化大小写问题

1. JSON 序列化key排序问题(fastjson)

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
//创建学生对象
Student student = new Student();
student.setName("小明");
student.setSex(1);
student.setAge(18);
//序列化 json key按字典排序
System.out.println(JSONObject.toJSONString(student ,SerializerFeature.MapSortField));
//过滤不要的key age
 System.out.println(JSONObject.toJSONString(student , new PropertyFilter() {
   public boolean apply(Object source, String name, Object value) {
    if ("age".equals(name)) {
     return false;
    }
    return true;
   }
  }, SerializerFeature.MapSortField));

2. JSON 序列化大小写问题(fastjson)

//学生类
import com.alibaba.fastjson.annotation.JSONField;
public class Student {
 private String name;
 private Integer sex;
 private Integer age;
 @JSONField(name = "Name") //用于序列化成json,key Name
 public String getName() {
  return name;
 }
 @JSONField(name = "Name") 用于json(Name)反序列化成学生对象
 public void setName(String name) {
  this.name = name;
 }
 public Integer getSex() {
  return sex;
 }
 public void setSex(Integer sex) {
  this.sex = sex;
 }
 public Integer getAge() {
  return age;
 }
 public void setAge(Integer age) {
  this.age = age;
 }
}

3. jackson 序列化大小写问题

@ResponseBody和@RequestBody中的序列化和反序列化就是用的jackson

 //学生类
import com.fasterxml.jackson.annotation.JsonProperty;
public class Student {
 @JsonProperty("Name")
 private String name;
 private Integer sex;
 private Integer age;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Integer getSex() {
  return sex;
 }
 public void setSex(Integer sex) {
  this.sex = sex;
 }
 public Integer getAge() {
  return age;
 }
 public void setAge(Integer age) {
  this.age = age;
 }
}
//自己测试下
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
@Test
public void test() throws Exception{
 Student student = new Student();
 student.setName("小明");
 ObjectMapper MAPPER = new ObjectMapper();
 //jackson序列化
 String json = MAPPER.writeValueAsString(student);
 System.out.println(json);
 //jackson反序列化
 Student student2 = MAPPER.readValue(json, Student.class);
 System.out.println(student2.getName());
}

4. jackson 序列化null值的问题

fastjson序列化默认会去掉值为null的键值对

//在学生类上加上这个
方式一:(已经过时的方法)
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
方式二:
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)

5. jackson 反序列化忽略多余的json字段

import com.fasterxml.jackson.databind.DeserializationFeature;
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

6. jackson 序列化忽略多余的json字段

方式一:
@JsonIgnoreProperties:该注解将在类曾级别上使用以忽略json属性。在下面的栗子中,我们将从albums的dataset中忽略“tag”属性;
@JsonIgnoreProperties({ "tags" })
方式二:
@JsonIgnore:该注释将在属性级别上使用以忽略特定属性;get方法上
@JsonIgnore 

7. jackson 常用注解

@JsonAlias("Name")  反序列化时生效
private String name;
@JsonProperty("Name") 反序列化和序列化都时生效
private String name;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

相关文章

  • Java微服务的打包问题解决

    Java微服务的打包问题解决

    本文主要介绍了Java微服务的打包问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • IntelliJ IDEA 2020.1 EAP4 发布,重命名/更改签名新功能一览

    IntelliJ IDEA 2020.1 EAP4 发布,重命名/更改签名新功能一览

    这篇文章主要介绍了IntelliJ IDEA 2020.1 EAP4 发布,重命名/更改签名新功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • Spring Boot Jar 文件能直接运行的操作方法

    Spring Boot Jar 文件能直接运行的操作方法

    本文将深入探讨 Spring Boot 的打包过程及其运行原理,揭示其 JAR 文件如何巧妙地集成依赖项、嵌入 Web 容器并实现自动配置,感兴趣的朋友跟随小编一起看看吧
    2024-12-12
  • Spring LDAP目录服务的使用示例

    Spring LDAP目录服务的使用示例

    本文主要介绍了Spring LDAP目录服务的使用示例
    2025-04-04
  • java 字符串池的深入理解

    java 字符串池的深入理解

    这篇文章主要介绍了java 字符串池的深入理解的相关资料,这里提供实例代码帮助大家学习理解这部分内容,希望大家能够掌握,需要的朋友可以参考下
    2017-08-08
  • springboot读取nacos配置文件的实现

    springboot读取nacos配置文件的实现

    SpringBoot注册服务到Nacos上,由Nacos来做服务的管理,本文主要介绍了springboot读取nacos配置文件的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • 在Java中如何避免创建不必要的对象

    在Java中如何避免创建不必要的对象

    作为Java开发者,我们每天创建很多对象,但如何才能避免创建不必要的对象呢?这需要我们好好学习,这篇文章主要给大家介绍了关于在Java中如何避免创建不必要对象的相关资料,需要的朋友可以参考下
    2021-10-10
  • 如何为Logback日志添加唯一追踪ID

    如何为Logback日志添加唯一追踪ID

    本文介绍了如何为Logback日志添加唯一追踪ID,以便在测试和调试时更容易定位报错信息,通过创建过滤器和修改配置文件,可以在每个请求的日志中添加唯一的ID,并将其返回给前端,这样,当用户反馈报错时,开发人员可以根据ID快速定位和解决问题
    2024-12-12
  • Spring  AOP的两种使用方法

    Spring  AOP的两种使用方法

    这篇文章主要介绍了Spring AOP的两种使用方法,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • Spring中@Primary注解的作用详解

    Spring中@Primary注解的作用详解

    这篇文章主要介绍了Spring中@Primary注解的作用详解,@Primary 注解是Spring框架中的一个注解,用于标识一个Bean作为默认的实现类,当存在多个实现类时,通过使用@Primary注解,可以指定其中一个作为默认的实现类,以便在注入时自动选择该实现类,需要的朋友可以参考下
    2023-10-10

最新评论