Java 如何将以下 JSON 字符串转换为 POJO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41499935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert the following JSON String to POJO
提问by Arsen Davtyan
I want to convert the following JSON
string to a Java
object:
我想将以下JSON
字符串转换为Java
对象:
{
"user": {
"0": {
"firstName": "Monica",
"lastName": "Belluci"
},
"1": {
"firstName": "John",
"lastName": "Smith"
},
"2": {
"firstName": "Owen",
"lastName": "Hargreaves"
}
}
}
To convert this to Java
object I've created the following classes:
为了将其转换为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
}
I'm using Hymansonlibrary to convert JSON
to Java
. Here is how I'm using the Hymanson for conversion:
我正在使用Hymanson库转换JSON
为Java
. 这是我如何使用 Hymanson 进行转换:
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonString, User.class);
The problem is that with this conversion above the Map
inside the User object is always empty. What am I doing wrong?
问题是上面的这个转换Map
里面的 User 对象总是空的。我究竟做错了什么?
Thanks in advance.
提前致谢。
采纳答案by K. Gol
I think it should work. I've executed this code and it works fine. Here is my example.
我认为它应该工作。我已经执行了这段代码,它工作正常。这是我的例子。
import com.fasterxml.Hymanson.databind.ObjectMapper;
import java.io.IOException;
public class TestHymanson {
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);
}
}
and the User.class:
和 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 + '\'' +
'}';
}
}
回答by Pratik Mohanrao Gondil
Use can done with the help of gson library.
使用可以在 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);
}
}
}
visit this linkhope this helps :)
访问此链接希望这会有所帮助:)
回答by lahirumw
You can try below code, It works fine..
你可以试试下面的代码,它工作正常..
public class User {
private Map<String, Map<String, String>> user;
public Map<String, Map<String, String>> getUser() {
return user;
}
public void setUser(Map<String, Map<String, String>> user) {
this.user = user;
}
}
public class JsonCast {
public static void main(String args[]) {
String response = "{\"user\" : {\"0\": {\"firstName\": \"Monica\",\"lastName\": \"Belluci\"},\"1\": { \"firstName\": \"John\",\"lastName\": \"Smith\"}}}";
ObjectMapper mapper = new ObjectMapper();
try {
User user = mapper.readValue(response, User.class);
System.out.println(user.getUser().get("0"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
回答by she12
I had one additional issue, that i faced when converting JSON to Java POJO: com.fasterxml.Hymanson.databind.JsonMappingException: Can not deserialize instance of class out of START_ARRAY token ...
我在将 JSON 转换为 Java POJO 时遇到了一个额外的问题: com.fasterxml.Hymanson.databind.JsonMappingException: Can not deserialize instance of class out of START_ARRAY token ...
If anyone faces this issue, this is because JSON is expecting an object {}
but it sees an array [{}]
within the JSON String that is passed in
如果有人遇到这个问题,这是因为 JSON 期望的是 anobject {}
但它array [{}]
在传入的 JSON 字符串中看到了
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonString, User.class);
To fix
修理
User[] user = mapper.readValue(jsonString, User[].class);
Reference: https://stackoverflow.com/a/33515796/4167786