如何在java中使用jackson在Json中动态更改Key的值

时间:2022-10-26 13:30:44

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。

#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。