使用 Jackson 将 JSON 字符串转换为 Java 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21640318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON string to Java object with Hymanson
提问by ViRALiC
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 Hymanson library which reads a JSON string from a file and assigns it to an object.
我对readValue(file, class)
在 Hymanson 库中找到的 ObjectMapper方法非常着迷,该方法从文件中读取 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 Hymanson, which allowed the following:
我想知道Hyman逊是否有某种方法,它允许以下内容:
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 Hymanson?
这是可行的还是在Hyman逊的限制范围内不存在这种方法?
采纳答案by Dan Ciborowski - MSFT
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);
回答by Davut Gürbüz
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. You can handle error cases on your own way.
我回来了null
,想稍后打印跟踪和检查空值。您可以按自己的方式处理错误情况。
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;
}
}
}