FasterXML杰克逊:JSON @符号? [英] FasterXML Jackson: The JSON @ symbol?

查看:195
本文介绍了FasterXML杰克逊:JSON @符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON:

"segmentid": {
    "mot": {
        "@displaytype": "B",
        "@type": "BLT",
        "#text": "Buss"
    },...

以下问题:


  • 什么是 @ 表示法?

  • 如果出于特定原因使用它,我如何在杰克逊中使用它?

  • What is the @ notation for?
  • If it is used for a specific reason, how can I use it in Jackson?

推荐答案

在这种情况下,它似乎只是一个普通的JSON版本。

In this case it seems to be just a normal JSON propety.

您可以使用班级中的 @JsonProperty 注释来获取它:

You can grab it by using the @JsonProperty annotation in your class:

public YourJacksonClass {
     @JsonProperty("@displayType")
     private String displayType;

     @JsonProperty("@type")
     private String type;

     @JsonProperty("#text")
     private String text;
}

或者,如果您使用的是 readTree 返回 JsonNode 的方法,只需正常访问:

Or, if you are using the readTree method which returns a JsonNode, just access it normally:

JsonNode node = mapper.readTree(...);
String type = node.get("segmentid").get("mot").get("@type");

当需要将类型信息序列化时,使用'@' - 前缀属性也很常见使用该对象。

It is also common to use '@'-prefixed properties when it is necessary to serialize type info together with the object.

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@type")

读取类型,BLT在您的情况下,在 ObjectMapper 中使用 TypeIdResolver 时,可以转换为实际类型。

The read type, "BLT" in your case, can then be transformed into an actual type when using a TypeIdResolver in your ObjectMapper.

这篇关于FasterXML杰克逊:JSON @符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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