Hig*_*ife 2 java json bing jackson
我想用Jackson来解析JSON Bing的结果,但我对如何使用它有点困惑.以下是Bing收到的JSON示例:
{
"SearchResponse":{
"Version":"2.2",
"Query":{
"SearchTerms":"jackson json"
},
"Web":{
"Total":1010000,
"Offset":0,
"Results":[
{
"Title":"Jackson JSON Processor - Home",
"Description":"News: 04-Nov-2011: Jackson 1.9.2 released; 23-Oct-2011: Jackson 1.9.1 released; 04-Oct-2011: Jackson 1.9.0 released (@JsonUnwrapped, value instantiators, value ...",
"Url":"http:\/\/jackson.codehaus.org\/",
"CacheUrl":"http:\/\/cc.bingj.com\/cache.aspx?q=jackson+json&d=4616347212909127&w=cbaf5322,11c785e8",
"DisplayUrl":"jackson.codehaus.org",
"DateTime":"2011-12-18T23:12:00Z",
"DeepLinks":"[...]"
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我真的只需要结果数组中的数据.这个数组可以有0到n个结果.有人可以提供一个例子来说明如何使用Jackson来反序化"结果"吗?
fge*_*fge 10
首先,将您的JSON读作树.ObjectMapper使用该readTree()方法实例化并读取您的JSON .
这会给你一个JsonNode.将结果作为另一个结果JsonNode并循环遍历数组:
final ObjectMapper mapper = new ObjectMapper();
final JsonNode input = mapper.readTree(...);
final JsonNode results = input.get("SearchResponse").get("Web").get("Results");
/*
* Yes, this works: JsonNode implements Iterable<JsonNode>, and this will
* cycle through array elements
*/
for (final JsonNode element: results) {
// do whatever with array elements
}
Run Code Online (Sandbox Code Playgroud)
您还可以考虑使用JSON Schema实现验证输入.无耻的插件:https://github.com/fge/json-schema-validator
| 归档时间: |
|
| 查看次数: |
3297 次 |
| 最近记录: |