java中jackson含义,使用Jackson在Java中解析JSON的子集

Given a Json, is it possible to use Jackson to only parse out a section of the message?

Say that the data I'm interested in is buried in a deep hierarchy of fields and I simply do not care about creating DTO classes for each and every class.

Given a very simplified scenario I'd like to model the Telephone class without knowing anything about the structure before it:

...{

"firstName": "John",

"lastName" : "doe",

"age" : 26,

"address" : {

"streetAddress": "naist street",

"city" : "Nara",

"postalCode" : "630-0192"

},

"phoneNumbers": [

{

"type" : "iPhone",

"number": "0123-4567-8888"

},

{

"type" : "home",

"number": "0123-4567-8910"

}

]

}....

I'm thinking something in the terms of using json-path together with deserializing just the parts I'm interested of. Some pseudo:

List phoneNrs = parse(".my.deep.structure.persons.phoneNumbers", List);

解决方案

ObjectMapper mapper = new ObjectMapper();

JsonNode json = mapper.readTree("... your JSON ...");

Using the JsonNode object you can then call get("my").get("deep").get("structure") to get the node you want.

Once you got your hands on that node, a simple call to mapper.treeToValue(myDeepJsonNode, Telephone[].class) will get you your array ofTelephone. You can get a list using a TypeReference as well.

To get to your deep JsonNode you can also use the findValue and findPath methods.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值