Java Jackson 映射器如何知道每个 Json 对象中的哪个字段分配给类对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22162916/
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
How does the Hymanson mapper know what field in each Json object to assign to a class object?
提问by itsmichaelwang
Let's say I have a Json object like this:
假设我有一个这样的 Json 对象:
{
"name": "Bob Dole",
"company": "Bob Dole Industries",
"phone": {
"work": "123-456-7890",
"home": "234-567-8901",
"mobile": "345-678-9012"
}
}
And to help me read it, I use Hymanson's Object Mapper with the following class:
为了帮助我阅读它,我使用 Hymanson 的 Object Mapper 和以下类:
public class Contact {
public static class Phone {
private String work;
private String home;
private String mobile;
public String getWork() { return work; }
public String getHome() { return home; }
public String getMobile() { return mobile; }
public void setWork(String s) { work = s; }
public void setHome(String s) { home = s; }
public void setMobile(String s) { mobile = s; }
}
private String name;
private String company;
private Phone phone;
public String getName() { return name; }
public String getCompany() { return company; }
public Phone getPhone() { return phone; }
public void setName(String s) { name = s; }
public void setCompany(String s) { company = s; }
public void setPhone(Phone p) { phone = p; }
}
My question is, how (using the simplest explanation possible), does the Object mapper "deserialize" the Json object? I thought it was matching variable names, but changing them by a few letters didn't affect the output. Then, I tried switching the order of the set() functions, but that didn't do anything. I also tried both, but that was also useless. I'm guessing there's something more sophisticated at work here, but what?
我的问题是,对象映射器如何(使用尽可能简单的解释)“反序列化”Json 对象?我认为它是匹配变量名称,但将它们更改为几个字母并不会影响输出。然后,我尝试切换 set() 函数的顺序,但这没有任何作用。我也试过这两种方法,但这也没有用。我猜这里有一些更复杂的东西在起作用,但是什么?
I tried to look in the documentation and past code, but I didn't see an explanation that made sense to me.
我试图查看文档和过去的代码,但没有看到对我有意义的解释。
采纳答案by itsmichaelwang
Without Annotations:
无注释:
Without any annotations, it does what is called POJO
mapping, it just uses reflectionon the instance members and uses some rules about how to map the keys in the json to the names of the instance members. *note: it works on private
members as well as public
or package protected
as well
没有任何注释,它执行所谓的POJO
映射,它只是对实例成员使用反射,并使用一些关于如何将 json 中的键映射到实例成员名称的规则。*注:它适用于private
成员以及public
或package protected
以及
If it doesn't match the names of the instance members, then it starts trying to match the getXXX
and setXXX
methods, if it doesn't match anything then it gives up.
如果它与实例成员的名称不匹配,则它开始尝试匹配getXXX
和setXXX
方法,如果它不匹配任何东西,则它放弃。
With Annotations:
带注释:
It uses the metadata supplied by the annotations to do the mapping and conversions.
它使用注释提供的元数据来进行映射和转换。
It is always better to explicitly use the annotations when you have the source to add them to, then there is no guess work on what gets mapped to what.
当您有将它们添加到其中的源时,明确使用注释总是更好,然后就没有关于什么被映射到什么的猜测工作。
Remember explicit is always better than implicit!
记住显式总是比隐式好!
This is all well documented on the WIKI:
这在 WIKI 上都有详细记录:
JSON Schema:
JSON 架构:
I am creating JSON Schema definitions for all my new projects now to document what is and isn't valid JSON according to the schema rules engine. It is a great way to document your data structures and eliminate parsing errors.
我现在正在为我所有的新项目创建 JSON 模式定义,以根据模式规则引擎记录什么是有效的 JSON 和无效的 JSON。这是记录数据结构和消除解析错误的好方法。