我想将以下JSON
字符串转换为Java
对象:
{
"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
}
我使用杰克逊库将JSON
转换为Java
。以下是我使用Jackson进行转换的方式:
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonString, User.class);
问题是,通过上面的转换,User对象内的Map
始终为空。我做错了什么?
提前致谢。
答案 0 :(得分:4)
我认为它应该有效。我执行了这段代码,工作正常。这是我的例子。
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:
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 + '\'' +
'}';
}
}
答案 1 :(得分: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);
}
}
}
访问此link希望这有助于:)
答案 2 :(得分:1)
你可以尝试下面的代码,它工作正常..
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();
}
}
}
答案 3 :(得分:0)
我在将JSON转换为Java POJO时面临一个额外的问题:com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of class out of START_ARRAY token ...
如果有人遇到此问题,这是因为JSON期望object {}
,但它会在传入的JSON字符串中看到array [{}]
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonString, User.class);
修复
User[] user = mapper.readValue(jsonString, User[].class);