如何使用GSON将json文件解析到Java POJO类中 [英] How to parse a json file into a java POJO class using GSON

查看:278
本文介绍了如何使用GSON将json文件解析到Java POJO类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GSON来解析JSON文件,并且我想将这个JSON对象映射到POJO类。问题是JSON中的属性名称没有骆驼大小写,但是我的java POJO对象具有骆驼属性名称。

I am using GSON to parse a JSON file and i want to map this JSON object to a POJO class. The problem is the property name in JSON doesn't have camel-case but my java POJO object having camel-case property name.

有没有任何想法没有任何性能击中?

Is there any idea without having any performance hit?

例如:JSON文件中的属性名称是'OrderNumber',但在我的POJO类中,改为ordernumber,我有'salesNumber'作为属性名称。现在我们该如何将JSON中的OrderNumber映射到POJO类的salesNumber?

Eg: attribute name in JSON file is 'OrderNumber', But in my POJO class , Instead ordernumber, i have 'salesNumber' as attribute name. Now how can we map that OrderNumber from JSON to salesNumber of POJO class?

感谢您!

Thanks in advance!

推荐答案

在这种情况下,您可以使用 @SerializedName 注释。

You can use @SerializedName annotation in this case.

private class SomeObject {

  @SerializedName("OrderNumber") 
  private String salesNumber;

}

这篇关于如何使用GSON将json文件解析到Java POJO类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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