How to traverse through a given json string
Recently, I came across a scenario that requires to traverse through the json property keys and values of a given json object.
Add below maven dependency into the pom file of the project.
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
Below three method will help to traverse through the json object
void iterateJSONObject(JSONObject jsonObject) {
jsonObject.keys().forEachRemaining(key -> {
Object value = jsonObject.get(key);
System.out.println("Key: " + key);
iterateValue(value);
});
}
void iterateJSONArray(JSONArray jsonArray) {
jsonArray.iterator().forEachRemaining(element -> {
iterateValue(element);
});
}
void iterateValue(Object value) {
if (value instanceof JSONObject) {
iterateJSONObject((JSONObject) value);
} else if (value instanceof JSONArray) {
iterateJSONArray((JSONArray) value);
} else {
System.out.println("Value: " + value);
}
}
Now you can call iterateJSONObject method to traverse through the json object as below,
String json = "{
"catalog":{
"product":{
"ProductImage":"cardigan.jpg",
"catalogItem":[
{
"gender":"Men's",
"catalogSize":[
{
"description":"Medium",
"colorSwatch":[
{
"Image":"red_cardigan.jpg",
"Content":"Red"
},
{
"Image":"burgundy_cardigan.jpg",
"Content":"Burgundy"
}
]
},
{
"description":"Large",
"dolorSwatch":[
{
"Image":"red_cardigan.jpg",
"Content":"Red"
},
{
"Image":"burgundy_cardigan.jpg",
"Content":"Burgundy"
}
]
}
],
"price":39.95,
"itemNumber":"QWZ5671"
}
],
"description":"Cardigan Sweater"
}
}
}"
JSONObject obj = new JSONObject(json);
newClass.handleJSONObject(obj);
Add below maven dependency into the pom file of the project.
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
Below three method will help to traverse through the json object
void iterateJSONObject(JSONObject jsonObject) {
jsonObject.keys().forEachRemaining(key -> {
Object value = jsonObject.get(key);
System.out.println("Key: " + key);
iterateValue(value);
});
}
void iterateJSONArray(JSONArray jsonArray) {
jsonArray.iterator().forEachRemaining(element -> {
iterateValue(element);
});
}
void iterateValue(Object value) {
if (value instanceof JSONObject) {
iterateJSONObject((JSONObject) value);
} else if (value instanceof JSONArray) {
iterateJSONArray((JSONArray) value);
} else {
System.out.println("Value: " + value);
}
}
Now you can call iterateJSONObject method to traverse through the json object as below,
String json = "{
"catalog":{
"product":{
"ProductImage":"cardigan.jpg",
"catalogItem":[
{
"gender":"Men's",
"catalogSize":[
{
"description":"Medium",
"colorSwatch":[
{
"Image":"red_cardigan.jpg",
"Content":"Red"
},
{
"Image":"burgundy_cardigan.jpg",
"Content":"Burgundy"
}
]
},
{
"description":"Large",
"dolorSwatch":[
{
"Image":"red_cardigan.jpg",
"Content":"Red"
},
{
"Image":"burgundy_cardigan.jpg",
"Content":"Burgundy"
}
]
}
],
"price":39.95,
"itemNumber":"QWZ5671"
}
],
"description":"Cardigan Sweater"
}
}
}"
JSONObject obj = new JSONObject(json);
newClass.handleJSONObject(obj);
Comments
Post a Comment