如何使用Jackson的ObjectMapper将JSON以下转换为POJO [英] How to convert below JSON to POJO using ObjectMapper of Jackson

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

问题描述

我正在尝试下面的代码,使用Jackson的ObjectMapper类将下面的JSON转换为POJO,但是它引发异常.谁能帮助我解决此问题. JSON实际上是由UI提供的,因此无法更改其格式.我需要使用杰克逊库将此JSON解析为java对象.

I am trying below code to convert below JSON to POJO using ObjectMapper class of Jackson but it's throwing exception. Could anyone help me to resolve this issue. Actually JSON is given by UI so can't change format of it. I need to parse this JSON to java object using Jackson library.

JSON:data.json

{
    "0": {
        "location": "6",
        "userType": "1",
        "isActive": "1",
        "userId": "Shailesh@gmail.com"
    },
    "1": {
        "location": "7",
        "userType": "2",
        "isActive": "1",
        "userId": "Vikram@gmail.com"
    }
}

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:由于输入结束,没有内容要映射

Exception: com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input

推荐答案

您可以使用杰克逊的ObjectMapper.readValue()方法.

You can use ObjectMapper.readValue() method of jackson.

我认为您的解决方案将是这样的:

I think your solution will be like this :

    String jsonBody = yourJson;
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        UserDetailsMapDao userDetailsMapDao = objectMapper
                .readValue(jsonBody, UserDetailsMapDao.class);
    } catch (JsonParseException e) {
        // TODO Exception Handling
    } catch (JsonMappingException e) {
        // TODO Exception Handling
    } catch (IOException e) {
        // TODO Exception Handling
    }

您的Daos将是这样的:

Your Daos will be like this :

公共类UserDetailsMapDao {

public class UserDetailsMapDao {

private Map<String, UserDetails> userDetailsMap;

public String getUserDetailsMap() {
return userDetailsMap;
}
public void setUserDetailsMap(String userDetailsMap) {
this.userDetailsMap = userDetailsMap;
}

}

公共类UserDetails {

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

}

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

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