简体   繁体   English

如何使用GSON将json文件解析为java POJO类

[英]How to parse a json file into a java POJO class using GSON

I am using GSON to parse a JSON file and i want to map this JSON object to a POJO class. 我正在使用GSON来解析JSON文件,我想将这个JSON对象映射到POJO类。 The problem is the property name in JSON doesn't have camel-case but my java POJO object having camel-case property name. 问题是JSON中的属性名称没有camel-case但我的java POJO对象具有camel-case属性名称。

Is there any idea without having any performance hit? 没有任何性能影响有什么想法吗?

Eg: attribute name in JSON file is 'OrderNumber', But in my POJO class , Instead ordernumber, i have 'salesNumber' as attribute name. 例如:JSON文件中的属性名称是'OrderNumber',但在我的POJO类中,而不是ordernumber,我有'salesNumber'作为属性名称。 Now how can we map that OrderNumber from JSON to salesNumber of POJO class? 现在我们如何将OrderNumber从JSON映射到POJO类的salesNumber?

Thanks in advance! 提前致谢!

You can use @SerializedName annotation in this case. 在这种情况下,您可以使用@SerializedName注释。

private class SomeObject {

  @SerializedName("OrderNumber") 
  private String salesNumber;

}

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

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