Reputation: 963
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
{
"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?
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
Upvotes: 1
Views: 1082
Reputation: 34460
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:
public class ServiceTypes {
private String storageName;
// etc
}
I´ve changed the visibility of the fields to private
, you need the corresponding getters and setters.
Upvotes: 0
Reputation: 116522
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 Map
s or List
s.
Upvotes: 1
Reputation: 62
Just go on and write the Storage class. Jackson will map the values automatically into your storage List via the Objectmapper.
One solution might be inheritance. You need multiple classes with different "IDs" but same properties.
Upvotes: 0