java Jackson 将字符串转换为对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43804503/
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
Hymanson Converting String to Object
提问by ytibrewala
Link.java
链接.java
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "rel", "href","method" })
public class Link {
@JsonProperty("rel")
private String rel;
@JsonProperty("href")
private String href;
@JsonProperty("method")
private Method method;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
I have this third party class with fasterxml Hymanson annotations. I can convert a given object into a string using the specified toString() method. Is there any way of using that String to get an object of type Link?
我有这个带有 fastxml Hymanson 注释的第三方类。我可以使用指定的 toString() 方法将给定的对象转换为字符串。有没有办法使用该字符串来获取链接类型的对象?
Note: The object itself has an embedded object (which has several more embedded objects) and these too needs to be converted into a Method object from the string itself.
注意:对象本身有一个嵌入对象(它有更多的嵌入对象),这些也需要从字符串本身转换为 Method 对象。
回答by ytibrewala
Just putting the comment by @pvpkiran in an answer.
只需将@pvpkiran 的评论放在答案中即可。
Use ObjectMapperclass from com.fasterxml.Hymanson.databind
使用com.fasterxml.Hymanson.databind 中的ObjectMapper类
ObjectMapper objectMapper = new ObjectMapper();
Converting from Object to String:
从对象转换为字符串:
String jsonString = objectMapper.writeValueAsString(link);
Converting from String to Object:
从字符串转换为对象:
Link link = objectMapper.readValue(jsonString, type)