资讯专栏INFORMATION COLUMN

将多个JSON字段映射到单个Java字段

printempw / 1313人阅读

摘要:简介本文中,教大家如何使用和将不同的字段映射到单个字段中。这两个注解将帮助我们把属性映射到同一字段。因此,将知道文档中映射到字段的其他字段的名称。

简介
本文中,教大家如何使用Jackson和Gson将不同的JSON字段映射到单个Java字段中。
Maven依赖

为了使用Jackson和Gson库,我们需要在POM中添加以下依赖项:


    com.google.code.gson
    gson
    2.8.5
    test


    com.fasterxml.jackson.core
    jackson-databind
    2.9.8
    test

示例JSON
假如,我们希望将不同位置的天气细节输入到我们的Java类中。我们发现了一些将天气数据发布为JSON文档的网站。但是,它们的格式并未是一致的
{
    "location": "广州",
    "temp": 15,
    "weather": "多云"
}
{
    "place": "深圳",
    "temperature": 35,
    "outlook": "晴天"
}

我们希望将这两种格式反序列化为同一个Java类,名为 Weather:

使用Jackson
为实现这一目标,我们将使用Jackson的@JsonProperty和@JsonAlias注释。这两个注解将帮助我们把JSON属性映射到同一Java字段。

首先,我们将使用@JsonProperty注释,以便让Jackson知道要映射的JSON字段的名称。在值@JsonProperty注解同时用于反序列化和序列化。

然后我们可以使用@JsonAlias注释。因此,Jackson将知道JSON文档中映射到Java字段的其他字段的名称。在用了@JsonAlias注释的属性用于反序列化。

@JsonProperty("location")
@JsonAlias("place")
private String location;
@JsonProperty("temp")
@JsonAlias("temperature")
private int temp;
 
@JsonProperty("outlook")
@JsonAlias("weather")
private String outlook;

Getter、Setter忽略

现在我们已经添加了注释,让我们使用Jackson的ObjectMapper方法创建Weather对象。

@Test
public void test() throws Exception {
 
    ObjectMapper mapper = new ObjectMapper();
 
    Weather weather = mapper.readValue("{
" 
      + "  "location": "广州",
"
      + "  "temp": 15,
"
      + "  "weather": "多云"
"
      + "}", Weather.class);
 
    TestCase.assertEquals("广州", weather.getLocation());
        TestCase.assertEquals("多云", weather.getOutlook());
        TestCase.assertEquals(15, weather.getTemp());
 
    weather = mapper.readValue("{
"
      + "  "place": "深圳",
"
      + "  "temperature": 35,
"
      + "  "outlook": "晴天"
"
      + "}", Weather.class);
 
   TestCase.assertEquals("深圳", weather.getLocation());
        TestCase.assertEquals("晴天", weather.getOutlook());
        TestCase.assertEquals(35, weather.getTemp());
}
使用Gson
现在,我们来看看Gson如何实现。我们需要在@SerializedName注释中使用值和  备用参数。

第一个将用作默认值,而第二个将用于指示我们要映射的JSON字段的备用名称:

@SerializedName(value="location", alternate="place")
private String location;
@SerializedName(value="temp", alternate="temperature")
private int temp;
 
@SerializedName(value="outlook", alternate="weather")
private String outlook;

现在我们已经添加了注释,让我们测试一下我们的例子:

@Test
public void test() throws Exception {
         
    Gson gson = new GsonBuilder().create();
    Weather weather = gson.fromJson("{
"
      + "  "location": "广州",
"
      + "  "temp": 15,
"
      + "  "weather": "多云"
"
      + "}", Weather.class);
         
    TestCase.assertEquals("广州", weather.getLocation());
        TestCase.assertEquals("多云", weather.getOutlook());
        TestCase.assertEquals(15, weather.getTemp());
         
    weather = gson.fromJson("{
"
      + "  "place": "深圳",
"
      + "  "temperature": 35,
"
      + "  "outlook": "晴天"
"
      + "}", Weather.class);
        
      TestCase.assertEquals("深圳", weather.getLocation());
        TestCase.assertEquals("晴天", weather.getOutlook());
        TestCase.assertEquals(35, weather.getTemp());
         
}
结论

我们通过使用Jackson的@JsonAlias或Gson的替代参数看到了这一点,我们可以轻松地将不同的JSON格式转换为相同的Java对象。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73814.html

相关文章

  • Elasticsearch Java High Level REST Client(Search A

    摘要:搜索请求用于与搜索文档聚合相关的任何操作,还提供了在结果文档上请求高亮的方法。将字段高光色添加到高亮构建器。稍后可以从中检索高亮的文本片段。 Search API 搜索请求 SearchRequest用于与搜索文档、聚合、suggestions相关的任何操作,还提供了在结果文档上请求高亮的方法。 在最基本的表单中,我们可以向请求添加查询: SearchRequest searchReq...

    liuhh 评论0 收藏0

发表评论

0条评论

printempw

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<