您的位置:首页 > 编程语言 > Java开发

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

2017-01-04 17:49 2346 查看
问题描述:

               项目中使用的是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>

写的很乱,只是作为记录,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json gson