簡體   English   中英

使用 Java 驅動程序從 POJO class 獲取 Mongodb ArrayOfObjects 值

[英]Getting a Mongodb ArrayOfObjects value from POJO class with Java driver

I am facing an issue(getting Null Value) while getting the Mongodb value from the ArrayOfObjects and My POJO class is like below:I have created separate POJOS for Each Object.Can anyone help me how to fetch the value of Email Systems.Bob.系統。 我的文檔:

"Email Systems" : [
        {
            "Bob" : {
                "System" : "Bob", 
                "result" : true
            }
        }, 
        {
            "Wild" : {
                "System" : "Wild", 
                "result" : true
            }
        },
        {
            "CRaft" : {
                "System" : "Craft", 
                "result" : false
            }
        }
    ]

我的 POJO Class:

public class Bob{
    @JsonProperty("System") 
    public String system;
    public String result;
}

public class Wild{
    @JsonProperty("System") 
    public String system;
    public String result;
}

public class CRaft{
    @JsonProperty("System") 
    public String system;
    public String result;
}

public class EmailSystem{
    @JsonProperty("Bob") 
    public Bob bob;
    @JsonProperty("Wild") 
    public Wild wild;
    @JsonProperty("CRaft") 
    public CRaft cRaft;
}

public class Root{
    @JsonProperty("EmailSystems") 
    public List<EmailSystem> emailSystems;
}

首先將 JSON 數據填充到您的 POJO 中,然后嘗試從中檢索,然后您將不會獲得 null 值。

確保在所有 POJO 中都有 GETTER/SETTER。 “結果”字段也是boolean 但你可以用字符串 map with("")

ObjectMapper obj = new ObjectMapper();

Root root = obj.readValue("jsonstring",Root.class);

they you can use getter to get the data.

POM 依賴:--

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.1</version>
</dependency>

您的 JSON 現在應該是:---(根據 POJO 類)

  {
  "root": {
    "EmailSystems": [
      {
        "Bob": {
          "System": "Bob",
          "result": "true"
        }
      },
      {
        "Wild": {
          "System": "Wild",
          "result": "true"
        }
      },
      {
        "CRaft": {
          "System": "Craft",
          "result": "false"
        }
      }
    ]
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM