简体   繁体   English

使用Jackson的JSON字符串到Java对象

[英]JSON string to Java object with Jackson

This is probably one of those questions where the title says it all. 这可能是标题说明的所有问题之一。

I am quite fascinated by the ObjectMapper's readValue(file, class) method, found within the Jackson library which reads a JSON string from a file and assigns it to an object. 我非常着迷于ObjectMapper的readValue(file, class)方法,该方法在Jackson库中找到,它从文件中读取JSON字符串并将其分配给对象。

I'm curious if this is possible to do by simply getting JSON from a string and applying it to an object. 我很好奇是否可以通过从字符串中获取JSON并将其应用于对象来实现。

Some sort of alternative readValue() method, which takes a String , instead of a file, and assigns it to an object? 某种替代的readValue()方法,它接受一个String而不是一个文件,并将它分配给一个对象?

For instance, while the default readValue(file, class) method looks like this: 例如,虽然默认的readValue(file, class)方法如下所示:

ObjectMapper mapper = new ObjectMapper();
Student student = mapper.readValue("C:\\student.json", Student.class);

I was wondering if there was some method in Jackson, which allowed the following: 我想知道杰克逊是否有某种方法允许以下方法:

ObjectMapper mapper = new ObjectMapper();
Student student = mapper.readValue("{\"id\":100,\"firstName\":\"Adam\"}", Student.class);

The second example takes a string and an object of a class while the first one takes a file and an object of a class. 第二个示例采用字符串和类的对象,而第一个示例采用文件和类的对象。

I just want to cut out the middle man, in this case, the file. 我只是想切出中间人,在这种情况下,文件。

Is this doable or does no such method exist within the constraints of Jackson? 这是可行的还是在杰克逊的限制范围内不存在这样的方法?

Try this, 试试这个,

You can't create a new string like your doing. 您无法像创建一样创建新字符串。

    String string = "{\"id\":100,\"firstName\":\"Adam\"}";
    Student student = mapper.readValue(string, Student.class);

And instead of handling errors in every mapper.readValue(json,Class) you can write a helper class which has another Generic method. 而不是在每个mapper.readValue(json,Class)中处理错误,你可以编写一个辅助类,它有另一个Generic方法。

and use 并使用

String jsonString = "{\"id\":100,\"firstName\":\"Adam\"}";
Student student = JSONUtils.convertToObject(jsonString,Student.class);

I'm returning null and fancying printing trace & checking null later on. 我正在返回null并且想要打印跟踪并稍后检查null。 You can handle error cases on your own way. 您可以按自己的方式处理错误情况。

public class JSONUtils {
    public static String convertToJSON(Object object)
    {
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String json;
        try {
            json = ow.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return convertToJSON(e);
        }
        return json;
    }


    public static <T> T convertToObject(Class<T> clazz,String jsonString)
    {
        try {
        ObjectMapper mapper = new ObjectMapper();
        return (T)mapper.readValue(jsonString, clazz);
        }catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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