如何将以下JSON字符串转换为POJO [英] How to convert the following JSON String to POJO

查看:138
本文介绍了如何将以下JSON字符串转换为POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下 JSON 字符串转换为 Java 对象:

I want to convert the following JSON string to a Java object:

{
  "user": {
    "0": {
      "firstName": "Monica",
      "lastName": "Belluci"
    },
    "1": {
      "firstName": "John",
      "lastName": "Smith"
    },
    "2": {
      "firstName": "Owen",
      "lastName": "Hargreaves"
    }
  }
}

将此转换为 Java 对象我已经创建了以下类:

To convert this to Java object I've created the following classes:

class User {
    private Map<String, MyObject> user = new HashMap<>();
    //Getter and Setter is here
}

class MyObject {
    private String firstName;
    private String lastName;
    //Getters and Setters are here
}

我正在使用杰克逊库将 JSON 转换为 Java 。以下是我如何使用Jackson进行转换:

I'm using Jackson library to convert JSON to Java. Here is how I'm using the Jackson for conversion:

ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonString, User.class);

问题是这个转换高于地图 User对象内部始终为空。我做错了什么?

The problem is that with this conversion above the Map inside the User object is always empty. What am I doing wrong?

提前致谢。

推荐答案

我认为它应该有效。我已经执行了这段代码,它运行正常。这是我的例子。

I think it should work. I've executed this code and it works fine. Here is my example.

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

public class TestJackson {

public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String testJson = "{\n" + "  \"user\": {\n" + "    \"0\": {\n" + "      \"firstName\": \"Monica\",\n" + "      \"lastName\": \"Belluci\"\n" + "    },\n" + "    \"1\": {\n" + "      \"firstName\": \"John\",\n" + "      \"lastName\": \"Smith\"\n" + "    },\n" + "    \"2\": {\n" + "      \"firstName\": \"Owen\",\n" + "      \"lastName\": \"Hargreaves\"\n" + "    }\n" + "  }\n" + "}";
        User readValue = mapper.readValue(testJson, User.class);
        System.out.println("readValue = " + readValue);
    }
}

和User.class:

and the User.class:

import java.util.HashMap;
import java.util.Map;

class User {
    private Map<String, MyObject> user = new HashMap<String, MyObject>();

    public Map<String, MyObject> getUser() {
        return user;
    }

    public void setUser(Map<String, MyObject> user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "User{" +
                "user=" + user +
                '}';
    }
}

class MyObject {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "MyObject{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

这篇关于如何将以下JSON字符串转换为POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆