本文主要是介绍接口请求与对象转json中字段大小写的处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、前端请求对象中,字段为大写的情况
》前端请求对象字段为大写
》后端接受对象字段也为大写
前后端对象字段一模一样,就是接受不到前端传过来的值,针对这种情况,只需在后端对象中加@JsonProperty("Id")
即可
如下所示:
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;@Data
public class AfterResume {@JsonProperty("Id")private String Id;/*** 起止时间*/@JsonProperty("YearAndMonth")private String YearAndMonth;/*** 在何单位任职*/@JsonProperty("OrganizationAndJob")private String OrganizationAndJob;
}
2、对象转json字符串,但对象中存在字段大写的情况,想要保持对象字段和json字符串格式一直;就不能用fastjson。这里有2中方式可供转换
1)通过com.fasterxml.jackson.databind.ObjectMapper
去转换,具体代码如下
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;public class Main {public static void main(String[] args) throws Exception {User user = new User("John", "Doe", 25);ObjectMapper mapper = new ObjectMapper();mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE); // 设置首字母大写的策略String json = mapper.writeValueAsString(AfterResume);System.out.println(json);}
}
2、 第二种通过gson去转换
String json = new Gson().toJson(afterResumes);
System.out.println(json);
这篇关于接口请求与对象转json中字段大小写的处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!