I am using Jackson library to process json data.
I have created a generic function to convert json string to a model class object:
public <T> T parseJsonToObject(String jsonStr, Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(jsonStr, clazz);
} catch (Exception e) {
}
return null;
}
I have a Person class:
public class Person{
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
//Getter & Setters ...
}
I got server response which is a List of persons:
[{"first_name":"John","last_name":"Smith"},{"first_name":"Kate","last_name":"Green"}]
My String jsonPersonsStr
holds the above json string.
Now I try to use my function to directly convert the json string to a List of Person:
List<Person> persons = parseJsonToObject(jsonPersonsStr, ArrayList.class);
But I got Exception:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.my.models.Person
How to modify my generic function to get rid of this Exception?
The best you can probably do is to support one generic method for Collection
types, and possibly others for non-Collection
types.
Here's a working version of the generic Collection
method:
import org.codehaus.jackson.map.type.TypeFactory;
//...
@SuppressWarnings({ "deprecation", "rawtypes" })
public static <E, T extends Collection> T parseJsonToObject(String jsonStr, Class<T> collectionType, Class<E> elementType) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(jsonStr, TypeFactory.collectionType(collectionType, elementType));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Then you would call this like:
List<Person> persons = parseJsonToObject(s, ArrayList.class, Person.class);
Try:
listOfObjects = mapper.readValue(connection.getInputStream(), new TypeReference<List<Person>>(){});
the problem here is due to Java's implementation on generics (List in this case)
Please check your JSON. Everything works fing if I try it with your Informations?
Try this static exsample. I define my own PersonList
(need no content)
public class JsonDeserializer {
public static void main(String[] args) throws IOException {
String jsonString = "[{\"first_name\":\"John\",\"last_name\":\"Smith\"},{\"first_name\":\"Kate\",\"last_name\":\"Green\"}]";
List<Person> persons = parseJsonToObject(jsonString, PersonList.class);
System.out.println("Persons: " + persons);
for (Person person : persons) {
System.out.println("Person " + person.firstName + " " + person.lastName);
}
}
public static class PersonList extends ArrayList<Person> {
}
public static <T> T parseJsonToObject(String jsonStr, Class<T> clazz) throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonStr, clazz);
}
public static class Person {
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
@Override
public String toString() {
return "Person{" + "firstName=" + firstName + ", lastName=" + lastName + '}';
}
}
}
UPDATE:
I use maven an this json lib
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.5</version>
<type>jar</type>
</dependency>
UPDATE:
Works also with Codehaus's jackson (1.7.3)
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.7.3</version>
</dependency>
I am using Jackson library to process json data.
I have created a generic function to convert json string to a model class object:
public <T> T parseJsonToObject(String jsonStr, Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(jsonStr, clazz);
} catch (Exception e) {
}
return null;
}
I have a Person class:
public class Person{
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
//Getter & Setters ...
}
I got server response which is a List of persons:
[{"first_name":"John","last_name":"Smith"},{"first_name":"Kate","last_name":"Green"}]
My String jsonPersonsStr
holds the above json string.
Now I try to use my function to directly convert the json string to a List of Person:
List<Person> persons = parseJsonToObject(jsonPersonsStr, ArrayList.class);
But I got Exception:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.my.models.Person
How to modify my generic function to get rid of this Exception?
The best you can probably do is to support one generic method for Collection
types, and possibly others for non-Collection
types.
Here's a working version of the generic Collection
method:
import org.codehaus.jackson.map.type.TypeFactory;
//...
@SuppressWarnings({ "deprecation", "rawtypes" })
public static <E, T extends Collection> T parseJsonToObject(String jsonStr, Class<T> collectionType, Class<E> elementType) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(jsonStr, TypeFactory.collectionType(collectionType, elementType));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Then you would call this like:
List<Person> persons = parseJsonToObject(s, ArrayList.class, Person.class);
Try:
listOfObjects = mapper.readValue(connection.getInputStream(), new TypeReference<List<Person>>(){});
the problem here is due to Java's implementation on generics (List in this case)
Please check your JSON. Everything works fing if I try it with your Informations?
Try this static exsample. I define my own PersonList
(need no content)
public class JsonDeserializer {
public static void main(String[] args) throws IOException {
String jsonString = "[{\"first_name\":\"John\",\"last_name\":\"Smith\"},{\"first_name\":\"Kate\",\"last_name\":\"Green\"}]";
List<Person> persons = parseJsonToObject(jsonString, PersonList.class);
System.out.println("Persons: " + persons);
for (Person person : persons) {
System.out.println("Person " + person.firstName + " " + person.lastName);
}
}
public static class PersonList extends ArrayList<Person> {
}
public static <T> T parseJsonToObject(String jsonStr, Class<T> clazz) throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonStr, clazz);
}
public static class Person {
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
@Override
public String toString() {
return "Person{" + "firstName=" + firstName + ", lastName=" + lastName + '}';
}
}
}
UPDATE:
I use maven an this json lib
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.5</version>
<type>jar</type>
</dependency>
UPDATE:
Works also with Codehaus's jackson (1.7.3)
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.7.3</version>
</dependency>
0 commentaires:
Enregistrer un commentaire