Q:
How to create
Q:
how to remove
/*
* This file is
The present disclo
Kentucky Football
---
abstract: 'Ion
It's just like a b
#
# Copyright 2017
Q:
What is the be
If this is your fiQ:
How to access values in a nested JSON format by giving a property name?
I am working with an API which returns the following JSON format as response:
{
"query": {
"count": 898,
"created": "2018-05-17T07:58:17Z",
"lang": "en-US",
"diagnostics": {
"results": [
{
"warningCount": 0,
"results": [
{
"answerKey": "5d6da1e3-a57d-4a1e-8025-9ee0acff4ac5",
"parent": "https://api.yelp.com/v3/businesses/BcIw8RmYCnLnE1TzpYh1wBh/answers/12353339",
"answer": {
"text": "3 Seasons Eatery & Bistro",
"quality": 10,
"rating": 3.6,
"yelp_business_id": "BcIw8RmYCnLnE1TzpYh1wBh",
"picture": "https://s3-media4.fl.yelpcdn.com/bphoto/D44G1sPwH-B6gBmJ1R1Kw/o.jpg",
"snippet_image_url": "https://s3-media3.fl.yelpcdn.com/photo/5R6K7l0xRGd7vG6tUu3WQ/ms.jpg"
}
}
],
"sorting": "rank"
},
{
"warningCount": 0,
"results": [
{
"answerKey": "5d6da1e3-a57d-4a1e-8025-9ee0acff4ac5",
"parent": "https://api.yelp.com/v3/businesses/BcIw8RmYCnLnE1TzpYh1wBh/answers/12353445",
"answer": {
"text": "3 Seasons Eatery & Bistro",
"quality": 10,
"rating": 3.6,
"yelp_business_id": "BcIw8RmYCnLnE1TzpYh1wBh",
"picture": "https://s3-media4.fl.yelpcdn.com/bphoto/D44G1sPwH-B6gBmJ1R1Kw/o.jpg",
"snippet_image_url": "https://s3-media3.fl.yelpcdn.com/photo/5R6K7l0xRGd7vG6tUu3WQ/ms.jpg"
}
}
],
"sorting": "rank"
},
I want to loop through it and print the value for the parent property.
So far I have this (I know this is not the right approach, that's why I wanted some tips):
import java.util.Scanner;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class JsonTest {
public static void main(String[] args) throws JsonParseException, JsonMappingException, ClassNotFoundException, IOException {
// Create ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
// set the module on the objectMapper
SimpleModule simpleModule = new SimpleModule("Module", new Version(1,0,0,null, null, null));
// set the customise feature for handling of Json to String
SimpleModule jsonSerializeModule = new SimpleModule("JsonSerialize", new Version(1,0,0,null, null, null));
jsonSerializeModule.addSerializer(JsonSerialize.class, new JsonSerializeSerializer());
simpleModule.addSerializer(JsonSerialize.class, new JsonSerializeSerializer());
simpleModule.setSerializationInclusion(JsonSerialize.class, SimpleModule.Feature.ALWAYS_INCLUDE);
// set the module on the objectMapper
objectMapper.registerModule(simpleModule);
String uri = "http://api.yelp.com/v3/businesses/12353445?fields=address,reviews.review_score";
System.out.println(objectMapper.readValue(uri, JsonTest.class));
}
}
class JsonSerializeSerializer implements Serializer {
@Override
public void serialize(JsonTest business, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeStringField("address", business.getAddress());
jgen.writeObject(new JsonTestResults(),"results");
jgen.writeObjectFieldStart("answerKey");
jgen.writeStringField("itemType", business.getId());
jgen.writeStringField("parent", business.getParent());
jgen.writeStringField("text", business.getAnswerText());
jgen.writeStringField("quality", business.getAnswerQuality());
jgen.writeStringField("rating", business.getRating());
jgen.writeStringField("yelp_business_id", business.getId());
jgen.writeStringField("picture", business.getImageUrl());
jgen.writeStringField("snippet_image_url", business.getImageUrl());
jgen.writeEndObject();
jgen.writeEndObject();
}
}
class JsonTestResults {
private List results;
public List getResults() {
return results;
}
public void setResults(List results) {
this.results = results;
}
}
class JsonTestResultsResults {
private String answerKey;
public String getAnswerKey() {
return answerKey;
}
public void setAnswerKey(String answerKey) {
this.answerKey = answerKey;
}
}
I am pretty new to JSON and Jackson. Any help would be really appreciated. Thanks.
A:
Use JsonPath
JsonPath is a java library that makes it easy to extract data from Json (and it can also do the opposite, by converting objects to json). In your case, it can look for JSON elements using a path, and return them as Java objects. This way, you can use it to create a Java object hierarchy that represents the nested structure of your JSON:
Map map = new ObjectMapper().readValue(result, Map.class);
String parent = map.get("query");
Map> mapResult = new ObjectMapper().readValue(parent, Map.class);
Map businessParent = mapResult.get("businesses");
Map> reviews = new ObjectMapper().readValue(businessParent.get("reviews"), Map.class);
Map answerKey = reviews.get("results").get("0").get("answerKey");
System.out.println(answerKey);
Prints:
5d6da1e3-a57d-4a1e-8025-9ee0acff4ac5