天天看点

java json gson 属性 大写 首字母 大写

问题描述:

               项目中使用的是fastjson,在使用过程中遇到如下问题,在于.net数据交换时,因为.net给的json数据格式的属性首字母都是大写字母,例如:

{Name:"guyue",Age=23,sex="男"},在转换json的时候,数据无法封装上去。

解决办法:

        修改json转换为gson,pom为<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.0</version></dependency>,在vo属性中增加标注,@SerializedName("Name"),即可,另外本系统使用的是springmvc,在配置文件中

<mvc:annotation-driven>

<mvc:message-converters register-defaults="true">

<bean

class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

<property name="objectMapper">

<bean class="com.fasterxml.jackson.databind.ObjectMapper">

<property name="serializationInclusion">

<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>

</property>

</bean>

</property>

</bean>

</mvc:message-converters>

</mvc:annotation-driven>

此配置为fastjson配置,如果想要接口输出也是通过gson,需要更改此配置,配置如下:

<mvc:annotation-driven>

<mvc:message-converters register-defaults="true">

<bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"/>

</mvc:message-converters>

</mvc:annotation-driven>

写的很乱,只是作为记录,