如何在java中使用jackson在Json中動態更改Key的值

[英]How to get dynamically changing Key's value in Json using jackson in java


below is my json string parsing using jackson using java , in which the storage array values will be dynamically changing based on the region , US, UK or any region. how to handle these objects

下面是我使用java使用jackson解析的json字符串,其中存儲數組值將根據地區,美國,​​英國或任何地區動態變化。如何處理這些對象

{
"serviceType": {
    "US - Northern California 1": [
      {
        "serviceName": "Virtual Private Cloud",
        "tenantType": "VP",
        "licenseType": "EDUCATIONAL",
        "description": "Virtual Private Cloud",
        "defaultFlag": "N",
        "tooltip": "",
        "storage": [
          {
            "ACC STORAGE": {
              "storageName": "SSD Accelerated Storage",
              "terms": [
                [
                  "12 months",
                  "12_MTH",
                  [
                    "Prepaid",
                    "Monthly"
                  ],
                  "N"
                ]
              ]
            }
          }
        ]
      }
    ],
    "US - New Jersey 1": [
      {
        "serviceName": "Air Dedicated Cloud",
        "tenantType": "DCP",
        "licenseType": "EDUCATIONAL",
        "description": "Air Dedicated Cloud",
        "defaultFlag": "N",
        "tooltip": "",
        "storage": [
          {
            "STD STORAGE": {
              "storageName": "Standard Storage",
              "terms": [
                [
                  "12 months",
                  "12_MTH",
                  [
                    "Monthly",
                    "Prepaid"
                  ]
              ]
            ]
            }
          }
        ]
      }
        ]

    }
}

My question is: how do I access the content of "storage" since "ACC STORAGE", "STD STORAGE", etc are all dynamic values?

我的問題是:如何訪問“存儲”的內容,因為“ACC STORAGE”,“STD STORAGE”等都是動態值?

  public class ServiceType 
{
    @JsonProperty("US - Northern California 1")
    public List<serviceTypes> USNorthernCalifornia1;
    @JsonProperty("US - New Jersey 1")
    public List<serviceTypes> uSNewJersey1;

     //getters and setters
}

public class serviceTypes
{
   public class serviceTypes 
{
    @JsonProperty("serviceName")
    public String serviceName;
    @JsonProperty("tenantType")
    public String tenantType;
    @JsonProperty("licenseType")
    public String licenseType;
    @JsonProperty("description")
    public String description;
    @JsonProperty("defaultFlag")
    public String defaultFlag;
    @JsonProperty("tooltip")
    public String tooltip;
    @JsonProperty("storage")
    public List<Storage> storage = new ArrayList<Storage>();
 //getters and setters
}

public class storage
{

}

the class storage left blank as i am confused wht to declare as the values will dynamically change

類存儲空白,因為我很困惑,因為值會動態改變

Please help me . thanks in advance

請幫幫我 。提前致謝

3 个解决方案

#1


1  

Usually this means that some properties use basic Map to contain values (where keys can be arbitrary Strings), instead of only using POJOs. Map values can still be POJOs, as well as Maps or Lists.

通常這意味着某些屬性使用基本Map來包含值(其中鍵可以是任意字符串),而不是僅使用POJO。地圖值仍然可以是POJO,也可以是地圖或列表。

#2


0  

Just go on and write the Storage class. Jackson will map the values automatically into your storage List via the Objectmapper.

繼續編寫Storage類。傑克遜將通過Objectmapper將值自動映射到您的存儲列表中。

One solution might be inheritance. You need multiple classes with different "IDs" but same properties.

一種解決方案可能是繼承。您需要具有不同“ID”但具有相同屬性的多個類。

#3


0  

I would simply use a Map.

我只想使用一張地圖。

public class ServiceTypes {

    private List<Map<String, Storage>> storage;

    // other fields ommited
}

Where the Storage class would now become:

Storage類現在將成為:

public class ServiceTypes {

    private String storageName;

    // etc
}

I´ve changed the visibility of the fields to private, you need the corresponding getters and setters.

我已將字段的可見性更改為私有,您需要相應的getter和setter。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/10/27/72550ab434a4f918024ff4089b622a12.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com