How to convert JSON string into List of Java object?

Nitesh :

This is my JSON Array :-

[ 
    {
        "firstName" : "abc",
        "lastName" : "xyz"
    }, 
    {
        "firstName" : "pqr",
        "lastName" : "str"
    } 
]

I have this in my String object. Now I want to convert it into Java object and store it in List of java object. e.g. In Student object. I am using below code to convert it into List of Java object : -

ObjectMapper mapper = new ObjectMapper();
StudentList studentList = mapper.readValue(jsonString, StudentList.class);

My List class is:-

public class StudentList {

    private List<Student> participantList = new ArrayList<Student>();

    //getters and setters
}

My Student object is: -

class Student {

    String firstName;
    String lastName;

    //getters and setters
}

Am I missing something here? I am getting below exception: -

Exception : com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.aa.Student out of START_ARRAY token
Manos Nikolaidis :

You are asking Jackson to parse a StudentList. Tell it to parse a List (of students) instead. Since List is generic you will typically use a TypeReference

List<Student> participantJsonList = mapper.readValue(jsonString, new TypeReference<List<Student>>(){});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert multiple attributes of object into List<String> using java 8

How to convert Java String to JSON Object

How to convert JSON to Java object?

Convert Json String to C# Object List

Convert List<Object> to String[] in Java

How to Convert List<String> to List<Object>

How to convert List to Json in Java

How to convert the following json string to java object?

how to convert object to string in java

How to convert the Object[] to String[] in Java?

How to convert Java object to JSON

How to convert String containing JSON object to actual JSON using Java

How to convert JSOn in the form of a String to List<String> in Java

How to convert this nested json string to a java object?

convert a string to a list of object using Java 8

Convert JSON to List<List<String>> in Java

How to convert an XML structured-like object to a JSON string in Java?

Spark convert JSON string to JSON object (Java)

Java.net(RestFul service JSON reponse parsing issue) How to convert list of same item in JSON reponse String to Java Object?

Convert a (String, List[(String, String)]) to JSON object

How to convert List(Of String) to Object()?

how to convert JSON string to object in JAVA android

How to Convert String Array JSON in a Java Object

Java how to convert List<Object[]> into a String[]

How to convert string to json string and object in java

How to convert a java object into a Json

Read String json array and convert to List object

How to convert String to Json Object in java or groovy

How to convert to XMl string to java object or json object