简体   繁体   中英

Deserialization Json to Java Object without Jackson library

I have JavaFX project without Maven and Gradle so i can not add jackson dependency. I need deserialize Json in String which I get from server to object Task. This is code for deserialization with Jackson library:

    private static Task convert(String json) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();

        Task task = objectMapper.readValue(json, Task.class);
        return task;
    }

But how to do that without that library?

Task class:

public class Task {
    private Long id;
    private String title;
    private String content;

    public Task(Long id, String title, String content) {
        this.id = id;
        this.title = title;
        this.content = content;
    }

    public Long getId() {
        return this.id;
    }

    public String getTitle() {
        return this.title;
    }

    public String getContent() {
        return this.content;
    }
}

You can still use the JSON-Jackson library (or any other library) even when you don't have Maven or Gradle. Go to Maven Central site and find Jason-Jackson artifact here . On the top-right corner, you will find a link to download the jars. Then you just add those jars to your environment and you can use the library. If you need to deploy it later, just make sure that you include those jars into your deployment. If you really insist on parsing your own JSON without 3d party library you will have to write your own parser. Here you can find the site that gives you JSON spec. Also, there are plenty of other libraries. The most simplistic one is JSON simple. You can download it fromhere (again top-right corner)

I think you were getting some errors for above code so you wanted to avoid this library.

If you add default constructer for Task class, it will work fine.

public class Test {

public static class Task {
    private Long id;
    private String title;
    private String content;

    public Task(){}

    public Task(Long id, String title, String content) {
        this.id = id;
        this.title = title;
        this.content = content;
    }

    public Long getId() {
        return this.id;
    }

    public String getTitle() {
        return this.title;
    }

    public String getContent() {
        return this.content;
    }
}

public static void main(String[] args) throws IOException {
    Task task = new Task(1L, "A", "C");
    String s = new ObjectMapper().writeValueAsString(task);
    System.out.println(s);
    Task convert = convert(s);
    System.out.println( new ObjectMapper().writeValueAsString(convert));
}
private static Task convert(String json) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    Task task = objectMapper.readValue(json, Task.class);
    return task;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM