如何使用Jackson的ObjectMapper将以下JSON转换为POJO

问题描述 投票:1回答:2

我正在尝试下面的代码,使用Jackson的ObjectMapper类将JSON转换为POJO,但它正在抛出异常。任何人都可以帮我解决这个问题。实际上JSON是由UI提供的,所以不能改变它的格式。我需要使用Jackson库将此JSON解析为java对象。

JSON:data.json

{
    "0": {
        "location": "6",
        "userType": "1",
        "isActive": "1",
        "userId": "[email protected]"
    },
    "1": {
        "location": "7",
        "userType": "2",
        "isActive": "1",
        "userId": "[email protected]"
    }
}

DTO的:

public class UsersList {
    List<UserDetails> users;
}

public class UserDetails {
    private String userId;
    private String location;
    private String userType;
    private String isActive;

    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public String getUserType() {
        return userType;
    }
    public void setUserType(String userType) {
        this.userType = userType;
    }
    public String getIsActive() {
        return isActive;
    }
    public void setIsActive(String isActive) {
        this.isActive = isActive;
    }
}

测试类:HandlerUtil

import java.io.IOException;
import java.io.InputStream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mcmcg.ams.lambda.model.UserDetails;

public class HandlerUtil {
    private static final Logger LOG = LogManager.getLogger(HandlerUtil.class);

    private HandlerUtil() {
    }

    public static void main(String[] args) {
        try (InputStream instream = HandlerUtil.class.getClassLoader().getResourceAsStream("data.json")) {
            UserDetails sample = new ObjectMapper().readValue(instream, UsersList.class);
            System.out.println(sample.toString());
        } catch (IOException ex) {
            LOG.error("Exception occurred while laoding data.json file : ", ex);
        }
    }
}

例外:com.fasterxml.jackson.databind.JsonMappingException:由于输入结束而没有要映射的内容

java json jackson pojo
2个回答
0
投票

JSON是以Map<String, UserDetails>的格式看看,关键0有用户"[email protected]"和关键1"[email protected]"

TypeReference<HashMap<String,UserDetails>> typeRef 
        = new TypeReference<HashMap<String,UserDetails>>() {};

HashMap<String,UserDetails> sample = new ObjectMapper()
                                  .readValue(instream, typeRef);

如果使用杰克逊使用@JsonAnySetter

public class UsersList {

private Map<String, UserDetails> users = new HashMap<>();

@JsonAnySetter
public void setUsers(String name, UserDetails value) {
this.addressDetails.put(name, value);
    }

 }

然后将其映射到UserDetails

 UserDetails sample = new ObjectMapper().readValue(instream, UsersList.class);

0
投票

首先,在将json字符串反序列化为DTO之前,DTO应包含无参数构造函数,getter和setter。您当前的DTO将匹配如下所示的字符串。

{
    "users": [
        {
            "location": "6",
            "userType": "1",
            "isActive": "1",
            "userId": "[email protected]"
        },
        {
            "location": "7",
            "userType": "2",
            "isActive": "1",
            "userId": "[email protected]"
        }
    ]
}

与上面提供的字符串示例匹配的DTO将如下所示。

public class UsersList {
    UserDetails zero;
    UserDetails one;

    public UsersList() {
    }

    public UserDetails getZero() {
        return zero;
    }

    public void setZero(final UserDetails zero) {
        this.zero = zero;
    }

    public UserDetails getOne() {
        return one;
    }

    public void setOne(final UserDetails one) {
        this.one = one;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.