使用 GSON 将 JSON 样式属性名称转换为 Java CamelCase 名称
我正在使用 GSON 将获取的 JSON 数据转换为 Java 对象。它在我的所有测试中都表现良好。 问题是我们的真实对象有一些名为 is_online 的属性。 GSON 仅在名称完全相等时才映射它们,最好让 GSON 将名称转换为 Java 驼峰式命名法 isOnline。
在创建 JSON 数据时似乎这是可能的,驼峰式大小写会转换为 JSON 中下划线分隔的单词。但我找不到一种方法来反过来指定这一点。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现以下设置在读取带有下划线属性的 json 并在模型中使用驼峰式命名时非常有效。
I have found the following setting works perfect when reading json with underscored attributes and using camelcasing in my models.
您可以使用
SerializedName
注释:注意:当您已经设置了
FieldNamingPolicy
时,SerializedName
将覆盖该特定字段的设置(相当对于特殊情况很方便)。You can use the
SerializedName
annotation:Note: When you have set a
FieldNamingPolicy
already,SerializedName
will overwrite its settings for that specific field (quite handy for special cases).请记住,您的示例是一种边缘情况。如果你有一个属性“foo”,它的 getter 应该命名为“getFoo”,如果你有一个名为“foo_bar”的属性,它的 getter 应该命名为“getFooBar”,但是,在你的示例中,你映射的是一个布尔值,并且布尔值有java 中的特殊情况命名约定。名为 online 的原始布尔属性应该有一个名为“isOnline”的 getter,而不是“getOnline”,甚至更糟糕的是“getIsOnline”。布尔包装对象(即 Boolean)不应该遵循这种特殊情况,并且名为“online”的属性应该有一个名为“getOnline”的 getter。
因此,名称中带有“is”的布尔属性是一种边缘情况,您需要在转换过程中删除这个特定的前缀。在相反的方向上,您的代码可能想要检查 json 对象的原始属性名称以及“is_XXX”版本。
Bear in mind your example is an edge case. If you have a property 'foo' its getter should be named 'getFoo', and if you have a property named 'foo_bar' its getter should be named 'getFooBar', however, in your example you're mapping a boolean and booleans have special case naming conventions in java. A primitive boolean property named online should have a getter named 'isOnline', NOT 'getOnline' or even worse, 'getIsOnline'. A boolean wrapper object (i.e. Boolean) should not follow this special case and a property named 'online' should have a getter named 'getOnline'.
Hence, having boolean properties with 'is' in the name is an edge case, where you'll want to strip out this particular prefix during your conversion. In the reverse direction, your code may want to inspect the json object for both a raw property name as well as a 'is_XXX' version.
我想你想要的是这里< /a>.使用注释,您可以告诉 GSON mySuperCoolField 实际上在 JSON 中称为 this_field_is_fun 并且它会正确解压它。至少我认为它也适用于反序列化。
如果这不起作用,您可以使用自定义 JsonSerializer/JsonDeserializers,它们效果很好,但是您必须针对类中的更改(例如添加字段时)更新它们。你失去了自动魔法。
最简单的事情(这会很难看,但如果第一个建议不起作用,那么非常干净和简单)就是简单地以让 GSON 满意的方式命名字段,并使用您喜欢的名称添加额外的访问器方法,例如
I think what you want is here. Using annotations you can tell GSON that the mySuperCoolField is actually called this_field_is_fun in the JSON and it will unpack it correctly. At least I think it works for deserialization too.
If that doesn't work, you can use custom JsonSerializer/JsonDeserializers, which work great, but you have to update them for changes in your class (like when you add a field). You lose the auto-magic.
The easiest thing to do (which would be ugly, but very clean and simple if the first suggestion doesn't work) would be to simply name the field in a way to make GSON happy, and add extra accessor methods with the names you like, e.g.