如何将以下 JSON 字符串转换为 POJO

2022-09-03 04:57:32

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

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

为了将其转换为对象,我创建了以下类:Java

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
}

我正在使用杰克逊库转换为.以下是我如何使用Jackson进行转换:JSONJava

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

问题在于,通过此转换,内部的 User 对象始终为空。我做错了什么?Map

提前致谢。


答案 1

我认为它应该起作用。我已经执行了这段代码,它工作正常。这是我的例子。

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

和用户.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 + '\'' +
                '}';
    }
}

答案 2

使用可以在gson库的帮助下完成。

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonToJava {

    public static void main(String[] args) throws IOException {
        try(Reader reader = new InputStreamReader(JsonToJava.class.getResourceAsStream("/Server2.json"), "UTF-8")){
            Gson gson = new GsonBuilder().create();
            Person p = gson.fromJson(reader, YourPOJOClass.class);
            System.out.println(p);
        }
    }
}

访问此链接,希望这有助于:)