【问题标题】:how to mapped java embbeded object with JSON如何使用 JSON 映射 java 嵌入对象
【发布时间】:2019-10-10 03:01:10
【问题描述】:

我构建了一个谷歌日历 API,但我想不明白我的 json 文件的要点。

我成功地用我的 json 文件创建了我的 java 对象,但问题是:

我有两个班级:

public class User {
    private String email;
    private String firstname;
    private String lastname;
    Entity entity;
 ``

and my Entity

``  public class Entity {
    private String  name;
    private String entityType;
    private Entity rootEntity;``

here my json file :
for user

``[
  {
    "firstname": "Jean-Marc",
    "lastname": "Chevereau",
    "email": "xxxxxxx@xxxxx.com",
    "entity": {
        "name":"BFA",
        "entityType":"secteur"
    }
  },
    {
    "firstname": "Florent",
    "lastname": "Hamlin",
    "email": "xxxxxxx@xxxxx.com",
     "entity": {
        "name":"IT",
        "entityType":"secteur"
    }
  },
  {
    "firstname": "Benoit",
    "lastname": "Micaud",
    "email": "xxxxxxx@xxxxx.com",
    "entity": {
        "name":"EX",
        "entityType":"offre",
        "rootEntity":{
            "name":"BFA"
          }
        }
      }
    ]``

And a Entity json file

```[ 
   {
    "name": "BFA",
    "entityType": "secteur",
    "rootEntity": "",

  },
  {
   "name": "EX",
    "entityType": "Offre",
    "rootEntity": "BFA",
    }
  }
]

但麻烦就在这里。如果在我的 User.json 中我写实体名称,我不想写实体类型和根实体,因为如果我写实体名称是 BFA,它将始终是相同的实体类型和根实体。 换句话说,我的 json Entity 将永远是相同的,如果我只是把我们知道的名称指向一个实体对象。

例如,在这个 user.json 文件中,我只需要将

[
  {
    "firstname": "Jean-Marc",
    "lastname": "Chevereau",
    "email": "xxxxxxx@xxxxx.com",
    "entity": {
        "name":"BFA",

    }
  },
    {
    "firstname": "Florent",
    "lastname": "Hamlin",
    "email": "xxxxxxx@xxxxx.com",
     "entity": {
        "name":"IT",

    }
  },
  {
    "firstname": "Benoit",
    "lastname": "Micaud",
    "email": "xxxxxxx@xxxxx.com",
    "entity": {
        "name":"EX",
    }
  }
]

【问题讨论】:

    标签: java json object


    【解决方案1】:

    Json-lib 中,您有一个JsonConfig 来指定允许的字段:

    JsonConfig jsonConfig=new JsonConfig();
    jsonConfig.registerPropertyExclusion(Entity.class,"rootEntity");
    jsonConfig.registerPropertyExclusion(Entity.class,"entityType");
    
    JSON json = JSONSerializer.toJSON(objectToWrite,jsonConfig);
    

    【讨论】:

    • 我正在使用jackson lib,但我不明白,为什么要排除属性?在那种情况下,我只是不把它写在 json 文件上,它也能正常工作。我的愿望是将 user.file 对象中的实体名称映射到已设置其属性的实体对象
    • @RaphaelObadia 您没有写到您的问题是将 JSON 反序列化为 Java。从“i dont want to write entitytype and rootEntity”我们假设您想在序列化时排除某些部分。在 Jackson 中,您可以为 Entity 类定义和注册自己的 Deserializer
    • 很好,我只是不知道反序列化是我的问题:D。现在我知道去哪里找了。
    【解决方案2】:

    我想 com.fasterxml.jackson 的 @JsonIgnore 注释应该会有所帮助。

    public class Entity {
        private String  name;
        @JsonIgnore
        private String entityType;
        @JsonIgnore
        private Entity rootEntity;
    }
    

    【讨论】:

    • 我不会将数据放入数据库中,我的目标是构建列表用户的对象并将其用于 api
    • 这是Jackson 库的一部分。
    猜你喜欢
    • 1970-01-01
    • 2014-11-02
    • 2013-01-26
    • 2016-12-12
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多