【发布时间】:2012-12-20 15:14:52
【问题描述】:
我有一个从 solr 实例返回的 JSON 响应......
{"responseHeader":
{"status":0,"QTime":1,"params":{"sort":"score asc","fl":"*,score",
"q":"{! score=distance}","wt":"json","fq":"description:motor","rows":"1"}},
"response":{"numFound":9,"start":0,"maxScore":6.8823843,"docs":
[{"workspaceId":2823,"state":"MN","address1":"1313 mockingbird Lane",
"address2":"","url":"http://mydomain.com/","city":"Minneapolis",
"country":"US","id":"399068","guid":"","geo":["45.540239, -98.580473"],
"last_modified":"2012-12-12T20:40:29Z","description":"ELEC MOTOR",
"postal_code":"55555","longitude":"-98.580473","latitude":"45.540239",
"identifier":"1021","_version_":1421216710751420417,"score":0.9288697}]}}
我正在尝试将其映射到 java 对象:
public class Item extends BaseModel implements Serializable {
private static final long serialVersionUID = 1L;
protected Integer workspaceId;
protected String name;
protected String description;
protected String identifier;
protected String identifierSort;
protected Address address;
protected String url;
/** getters and setters eliminated for brevity **/
}
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
protected String address1;
protected String address2;
protected String city;
protected String state;
protected String postalCode;
protected String country;
/** getters and setters eliminated for brevity **/
}
如何将 address1、address2、city、state 等映射到 Item 对象中的 Address 对象中?我一直在阅读有关 Jackson annotations 的信息,但我没有真正想到从哪里开始。
【问题讨论】:
-
您必须指定您尝试过的内容。有不同的方法mattgemmell.com/2008/12/08/what-have-you-tried
-
您考虑过使用 SolrJ 吗?您可能会发现这对于展开响应和构造查询都更方便。
-
@Eric 我没有但完全愿意调查它。你有一个你认为是一个很好的起点吗?
-
@EricWilson SolrJ 确实有办法映射到 POJO,但没有(我能找到)映射到像我这样的嵌套子对象。