This is my JSON Array :-

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

我的字符串对象中有这个.现在我想将其转换为Java对象,并将其存储在Java对象列表中.e、 g.在学生对象中.

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
}

我的学生目标是:-

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

推荐答案

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>>(){});

Json相关问答推荐

如何在Power BI中集成API和JSON数据后将数据转换为表?

使用单元和非单元版本反序列化Rust中的枚举,而无需编写自定义反序列化程序

使用json_query更新事实

Jolt-在数组列表中插入新的字段

如何使用模式注册中心创建从主题中取消本地化的ks qlDB表?

Jolt Spec 跳过数组中的第一个元素

删除 JOLT 中的方括号

JOLT分裂和数组数据

我如何将 JSON 格式与 flutter 一起使用?帮助使用 Gamebanana api

将 json 文件转换为 json 对象会打乱对象的顺序

Oracle Apex - 将 JSON 对象分配给变量以返回

如何限制resolveJsonModule的范围?

为什么JsonConvert反序列化对象以int但不长失败

使用基本身份验证通过 CURL 发布 JSON

SyntaxError:Object.parse(本机)AngularJS中的意外标记o

在 JavaScript 中从 Json 数据中删除反斜杠

按 JSON 数据类型 postgres 排序

将带有数据和文件的 JSON 发布到 Web Api - jQuery / MVC

在 JSON 反序列化期间没有为System.String类型定义无参数构造函数

在 Rails 3 中处理 JS/ERB 模板中的 JSON