简体   繁体   English

GSON 可以以不区分大小写的方式反序列化吗

[英]Can GSON deserialize in a case-insensitive way

In prototyping communication between .NET desktop app and Java server, using REST with JSON posts, I am running into a case-sensitivity issue.在 .NET 桌面应用程序和 Java 服务器之间的原型通信中,使用带有 JSON 帖子的 REST,我遇到了区分大小写的问题。 The .NET objects have there properties in Pascal Casing (which is conventional for .NET), eg: Symbol, EntryValue (etc), while the Java representation of same object uses camel casing, eg symbol, entryValue . .NET 对象在 Pascal Casing(这是 .NET 的常规)中有一些属性,例如: Symbol, EntryValue (等),而同一对象的 Java 表示使用驼峰式大小写,例如symbol, entryValue

The server receives json value as:服务器接收 json 值如下:

{"EntrySize":100,"Symbol":"AMZN"}

But Gson doesn't deserialize in case-insensitive manner.但是 Gson 不会以不区分大小写的方式反序列化。 Is there any way to get Gson to do this?有没有办法让 Gson 做到这一点?

Use FieldNamingPolicy on a GsonBuilder , to get your Gson object.GsonBuilder上使用FieldNamingPolicy来获取您的Gson对象。 Yours seems to match UPPER_CAMEL_CASE .你的似乎匹配UPPER_CAMEL_CASE

Gson gson = new GsonBuilder()
        .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
        .create();

For any exceptions, annotate your class field with a @SerializedName annotation.对于任何例外情况,请使用@SerializedName注释来注释您的类字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM