Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jackson deserialization json to java-objects

Here is my Java code which is used for the de-serialization, i am trying to convert json string into java object. In doing so i have used the following code:

package ex1jackson; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; public class Ex1jackson { public static void main(String[] args) {    ObjectMapper mapper = new ObjectMapper(); try {         String userDataJSON = "[{\"id\":\"value11\",\"name\": \"value12\",\"qty\":\"value13\"},"                               + "{\"id\": \"value21\",\"name\":\"value22\",\"qty\": \"value23\"}]";         product userFromJSON = mapper.readValue(userDataJSON, product.class);         System.out.println(userFromJSON);     } catch (JsonGenerationException e) {         System.out.println(e);         } catch (JsonMappingException e) {        System.out.println(e);     } catch (IOException e) {     System.out.println(e);     }  } } 

and my product.java class

package ex1jackson; public class product  { private String id; private String name;  private String qty;   @Override public String toString() {     return "Product [id=" + id+ ", name= " + name+",qty="+qty+"]"; } } 

i am getting the following error.

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:  Unrecognized field "id" (class ex1jackson.product), not marked as ignorable (0 known properties: ]) at  [Source: java.io.StringReader@16f76a8; line: 1, column: 8] (through reference chain: ex1jackson.product["id"])  BUILD SUCCESSFUL (total time: 0 seconds) 

help me to solve this,

like image 449
MAHI Avatar asked Feb 15 '13 07:02

MAHI


People also ask

Can we convert JSON to Java object?

We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.

How do you serialize an object to JSON Jackson in Java?

Converting Java object to JSON In it, create an object of the POJO class, set required values to it using the setter methods. Instantiate the ObjectMapper class. Invoke the writeValueAsString() method by passing the above created POJO object. Retrieve and print the obtained JSON.

Does Jackson use Java serialization?

Note that Jackson does not use java.


2 Answers

It looks like you are trying to read an object from JSON that actually describes an array. Java objects are mapped to JSON objects with curly braces {} but your JSON actually starts with square brackets [] designating an array.

What you actually have is a List<product> To describe generic types, due to Java's type erasure, you must use a TypeReference. Your deserialization could read: myProduct = objectMapper.readValue(productJson, new TypeReference<List<product>>() {});

A couple of other notes: your classes should always be PascalCased. Your main method can just be public static void main(String[] args) throws Exception which saves you all the useless catch blocks.

like image 85
Steven Schlansker Avatar answered Sep 18 '22 11:09

Steven Schlansker


You have to change the line

product userFromJSON = mapper.readValue(userDataJSON, product.class); 

to

product[] userFromJSON = mapper.readValue(userDataJSON, product[].class); 

since you are deserializing an array (btw: you should start your class names with upper case letters as mentioned earlier). Additionally you have to create setter methods for your fields or mark them as public in order to make this work.

Edit: You can also go with Steven Schlansker's suggestion and use

List<product> userFromJSON =         mapper.readValue(userDataJSON, new TypeReference<List<product>>() {}); 

instead if you want to avoid arrays.

like image 42
nutlike Avatar answered Sep 19 '22 11:09

nutlike