Reputation: 9
I am new to JSON
, and would appreciate it if someone can help me with parsing this.
It's like parsing the array first then the object and then parsing the stuff inside the attributes array.
Any advice where I can start?
This code is how I am getting the json file
String json = IOUtils.toString(response.getEntity().getContent());
System.out.println(json);
Shown is a sample of what I need to parse. I would need the following information:
"name":"",
"attributeName": "Alternate Name",
"attributeValue": "",
"attributeName": "Functional Area",
"attributeValue": "N/A"
[
{
"id": 1234,
"name": "",
"formId": 34,
"sortOrder": 0,
"attributes": [
{
"id": 67899,
"attribute": {
"attributeName": "Alternate Name",
"attributeType": "Text",
"id": 894
},
"attributeValue": ""
},
{
"id": 67185,
"attribute": {
"attributeName": "Description",
"attributeType": "TextArea",
"id": 900
},
"attributeValue": ""
},
{
"id": 11345,
"attribute": {
"attributeName": "Functional Area",
"attributeType": "DropdownList",
"id": 902,
"values": [
{
"id": 3471,
"sortOrder": 0,
"attributeValue": "N/A"
},
{
"id": 3472,
"sortOrder": 1,
"attributeValue": "ES"
},
{
"id": 3473,
"sortOrder": 2,
"attributeValue": "IPC"
},
{
"id": 3474,
"sortOrder": 3,
"attributeValue": "ISS"
},
{
"id": 3475,
"sortOrder": 4,
"attributeValue": "TECH"
}
]
},
"attributeValue": "N/A"
]
and then there's another array (similar to above) that need to be parsed. It's a huge file to be parsed the same way.
Thank you in advance
Upvotes: 0
Views: 86
Reputation: 1626
Use a tool like Jackson or Gson, both available in Maven.
For example, using Jackson:
MyData data = new ObjectMapper().reader(MyData.class).readValue(response.getEntity().getContent());
data.getName(); // "name":""
data.getAttributes().get(0).getAttribute().getName(); // "attributeName": "Alternate Name"
data.getAttributes().get(0).getAttribute().getValue(); // "attributeValue": "",
data.getAttributes().get(2).getAttribute().getName(); // "attributeName": "Functional Area"
data.getAttributes().get(2).getAttribute().getValue(); // "attributeValue": "N/A",
// If you want to get fancy
data.getAttribute("Alternate Name").getValue();
data.getAttribute("Functional Area").getValue();
To do the above, you need the following mapping classes. You could deserialize into a Map
and then dig around with get
and casts, but I prefer Java objects with proper methods and types. The @JsonIgnoreProperties
annotation allows you to map only those JSON fields you care about.
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyData {
@JsonProperty
private String name;
@JsonProperty
private List<Attribute> attributes;
public String getName() {
return name;
}
public List<Attribute> getAttributes() {
return attributes;
}
// If you want to get fancy
public AttributeSpec getAttribute(String name) {
for(Attribute attr : attributes) {
if(name.equals(attr.getName())) {
return attr;
}
}
throw new IllegalArgumentException("Unknown name " + name);
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attribute {
@JsonProperty
private AttributeSpec attribute;
public AttributeSpec getAttribute() {
return attribute;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class AttributeSpec {
@JsonProperty
private String attributeName;
@JsonProperty
private String attributeValue;
public String getName() {
return attributeName;
}
public String getValue() {
return attributeValue;
}
}
Upvotes: 1