Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON in Spring MVC using Jackson JSON

Ok, so I've been looking at this for a little while now and am no further on. I've got a Spring MVC servlet that I need to accept JSON from a JavaScript front end web app. To parse the JSON I need to use Jackson. I need to take the values within the JSON and store them into a List in the order they appear in the JSON. I've tried using the JsonFactory with the JsonParser and JsonNode objects but can quite get it to work. I've also tried to just open a BufferedReader and iterate through the request body line by line but again can't quite get this either. I've looked at a couple of related questions on here, but none so far have worked for me.

Could anyone in the know point me in the right direction here please, a web page with an example would be great!

like image 825
MeanwhileInHell Avatar asked May 16 '11 15:05

MeanwhileInHell


People also ask

How do I read a JSON file in Jackson?

databind. ObjectMapper ) is the simplest way to parse JSON with Jackson. The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON.

What is Jackson JSON parser?

Jackson JSON Parser API provides easy way to convert JSON to POJO Object and supports easy conversion to Map from JSON data. Jackson supports generics too and directly converts them from JSON to object.

How does Jackson read nested JSON?

A JsonNode is Jackson's tree model for JSON and it can read JSON into a JsonNode instance and write a JsonNode out to JSON. To read JSON into a JsonNode with Jackson by creating ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of JsonNode class.

Does Jackson support JsonPath?

The Jayway JsonPath library has support for reading values using a JSON path. If you would like to specifically use GSON or Jackson to do the deserialization (the default is to use json-smart), you can also configure this: Configuration.


1 Answers

The whole point of using a mapping technology like Jackson is that you can use Objects (you don't have to parse the JSON yourself).

Define a Java class that resembles the JSON you will be expecting.

e.g. this JSON:

{ "foo" : ["abc","one","two","three"], "bar" : "true", "baz" : "1" } 

could be mapped to this class:

public class Fizzle{     private List<String> foo;     private boolean bar;     private int baz;     // getters and setters omitted } 

Now if you have a Controller method like this:

@RequestMapping("somepath") @ResponseBody public Fozzle doSomeThing(@RequestBody Fizzle input){     return new Fozzle(input); } 

and you pass in the JSON from above, Jackson will automatically create a Fizzle object for you, and it will serialize a JSON view of the returned Object out to the response with mime type application/json.

For a full working example see this previous answer of mine.

like image 118
Sean Patrick Floyd Avatar answered Oct 02 '22 11:10

Sean Patrick Floyd