Json parsing help in java

It would be possible if someone helped me parse this json result. I got the result as a string

{"query":{"latitude":39.9889,"longitude":-82.8118},"timestamp":1310252291.861,"address":{"geometry":{"coordinates":[-82.81168367358264,39.9887910986731],"type":"Point"},"properties":{"address":"284 Macdougal Ln","distance":"0.02","postcode":"43004","city":"Columbus","county":"Franklin","province":"OH","country":"US"},"type":"Feature"}} 
+4
source share
4 answers

Jackson Simple and intuitive to use. For me, the best available. Start with Simple Data Binding , it will throw out everything it finds in Maps and Lists.

Like this:

 ObjectMapper mapper = new ObjectMapper(); Map<String,Object> yourData = mapper.readValue(new File("yourdata.json"), Map.class); 

That is all that is needed.

A good and quick introduction can be found here.

And a complete working example with your actual data:

 import java.io.IOException; import java.util.Map; import org.codehaus.jackson.map.ObjectMapper; public class Main { public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); Map<?,?> rootAsMap = mapper.readValue( "{\"query\":{\"latitude\":39.9889,\"longitude\":-82.8118},\"timestamp\":1310252291.861,\"address\":{\"geometry\":{\"coordinates\":[-82.81168367358264,39.9887910986731],\"type\":\"Point\"},\"properties\":{\"address\":\"284 Macdougal Ln\",\"distance\":\"0.02\",\"postcode\":\"43004\",\"city\":\"Columbus\",\"county\":\"Franklin\",\"province\":\"OH\",\"country\":\"US\"},\"type\":\"Feature\"}}".getBytes(), Map.class); System.out.println(rootAsMap); Map query = (Map) rootAsMap.get("query"); Map address = (Map) rootAsMap.get("address"); Map addressProperties = (Map) address.get("properties"); String county = (String) addressProperties.get("county"); System.out.println("County is " + county); } } 

Now all this network manipulation also illustrates the Bojo point very well, using full binding (by creating a Java class that reflects the contents of the JSON data) will work better in the end.

+8
source

The two best options that I know of:

Using them is a call to a single mapping method. But remember that since Java is statically typed, you may need to create an object with the required structure. (This is not necessary for you, but it seems more natural)

+3
source

With Jackson, I would take the next approach. Since the coordinates in JSON come in two different formats - sometimes an object, sometimes an array - the solution is softly complicated by the necessary deserialization processing.

 import java.io.File; import java.io.IOException; import java.math.BigDecimal; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.ObjectCodec; import org.codehaus.jackson.Version; import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; import org.codehaus.jackson.map.DeserializationContext; import org.codehaus.jackson.map.JsonDeserializer; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.module.SimpleModule; import org.codehaus.jackson.node.ArrayNode; public class JacksonFoo { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY)); mapper.registerModule( new SimpleModule("CoordinatesDeserializer", Version.unknownVersion()) .addDeserializer(Coordinates.class, new CoordinatesDeserializer())); Result result = mapper.readValue(new File("input.json"), Result.class); System.out.println(mapper.writeValueAsString(result)); } } class CoordinatesDeserializer extends JsonDeserializer<Coordinates> { @Override public Coordinates deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectCodec codec = jp.getCodec(); JsonNode node = codec.readTree(jp); if (node.isObject()) { ObjectMapper mapper = new ObjectMapper(); mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY)); return mapper.readValue(node, Coordinates.class); } // else it an array ArrayNode array = (ArrayNode) node; Coordinates coordinates = new Coordinates(); coordinates.latitude = codec.treeToValue(array.get(0), BigDecimal.class); coordinates.latitude = codec.treeToValue(array.get(1), BigDecimal.class); return coordinates; } } class Result { Coordinates query; BigDecimal timestamp; Address address; } class Coordinates { BigDecimal latitude; BigDecimal longitude; } class Address { String type; Geometry geometry; AddressDetails properties; } class Geometry { String type; Coordinates coordinates; } class AddressDetails { String address; BigDecimal distance; String postcode; String city; String county; String province; String country; } 
+1
source

All Articles