使用GSON将JSON样式属性名称转换为Java CamelCase名称 [英] Convert JSON style properties names to Java CamelCase names with GSON

查看:596
本文介绍了使用GSON将JSON样式属性名称转换为Java CamelCase名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 GSON 将我得到的JSON数据转换为Java对象。它在我所有的测试中都能很好地工作。
问题是,我们的真实对象有一些名为is_online的属性。 GSON只会映射它们,如果它们命名完全相同,那么让GSON将名称转换为Java驼峰案例isOnline会很好。

I'm using GSON to convert JSON data I get to a Java object. It works pretty well in all my tests. The problem is that our real objects have some properties named like is_online. GSON only maps them if they are named totally equal, it would be nice to have GSON convert the names to Java camel case isOnline.

看起来,创建JSON数据,骆驼大小写在JSON中被转换为下划线分隔的单词。但我无法找到一种方法来指定这种方式。

It seems this is possible while creating the JSON data, camel case is converted to underscore separated words in JSON. But I can't find a way to specify this the other way round.

推荐答案

我发现以下设置完美工作阅读带有下划线属性的json,并在我的模型中使用camelcasing。

I have found the following setting works perfect when reading json with underscored attributes and using camelcasing in my models.

Gson gson = new GsonBuilder()
    .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
    .create()

这篇关于使用GSON将JSON样式属性名称转换为Java CamelCase名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆