Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试将JSON字符串转换为JavaPOJO时遇到奇怪的问题 Maven依赖性-EclipseIDE 雇员阶级_Java_Json_Pojo - Fatal编程技术网

尝试将JSON字符串转换为JavaPOJO时遇到奇怪的问题 Maven依赖性-EclipseIDE 雇员阶级

尝试将JSON字符串转换为JavaPOJO时遇到奇怪的问题 Maven依赖性-EclipseIDE 雇员阶级,java,json,pojo,Java,Json,Pojo,将setter和getter添加到Employee类。将setter和getter添加到Employee类。您的Employee类中没有任何setter或getter吗?你是如何通过私有成员和无获取者将数据恢复的?无论如何,我怀疑Jackson正在使用no-arg构造函数实例化您的对象,并且由于没有setter,因此无法设置任何值。尝试添加setters,看看这是否解决了问题。@Jordan Added setters;问题已解决。您的Employee类中是否没有任何setter或getter?

将setter和getter添加到Employee类。

将setter和getter添加到Employee类。

您的
Employee
类中没有任何setter或getter吗?你是如何通过私有成员和无获取者将数据恢复的?无论如何,我怀疑Jackson正在使用no-arg构造函数实例化您的对象,并且由于没有setter,因此无法设置任何值。尝试添加setters,看看这是否解决了问题。@Jordan Added setters;问题已解决。您的
Employee
类中是否没有任何setter或getter?你是如何通过私有成员和无获取者将数据恢复的?无论如何,我怀疑Jackson正在使用no-arg构造函数实例化您的对象,并且由于没有setter,因此无法设置任何值。尝试添加setters,看看这是否解决了问题。@Jordan Added setters;问题解决了。
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.8</version>
        </dependency>
    import java.io.IOException;
    import com.fasterxml.jackson.core.JsonGenerationException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONtoJavaPOJO {
public static void main(String[] args) {
    String json = "{\"id\":1,\"name\":\"Lokesh Gupta\",\"age\":34,\"location\":\"India\"}";

    ObjectMapper mapper = new ObjectMapper();
    try
    {
        Employee emp = mapper.readValue(json, Employee.class);

        System.out.println(emp);
    }
    catch (JsonGenerationException e)
    {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;



//@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee
{
    private Integer id;
    private String name;
    private Integer age;
    private String location;



    public Employee() {
    }


    public Employee(Integer id, String name, Integer age, String location) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.location = location;
    }



    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", age=" + age
                + ", location=" + location + "]";
    }
}