使用Jackson读取JSON字符串的一部分 [英] Read part of a JSON String using Jackson

查看:199
本文介绍了使用Jackson读取JSON字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON字符串如下

{
   "rank":"-text_relevance",
   "match-expr":"(label 'star wars')",
   "hits":{
      "found":7,
      "start":0,
      "hit":[
         {"id":"tt1185834",
             "data":{
                "actor":["Abercrombie, Ian","Baker, Dee","Burton, Corey"],
                "title":["Star Wars: The Clone Wars"]
             }
         },
         .
         .
         .
         {"id":"tt0121766",
             "data":{
                "actor":["Bai, Ling","Bryant, Gene","Castle-Hughes, Keisha"],
                "title":["Star Wars: Episode III - Revenge of the Sith"]
             }
         }
       ]
    },
    "info":{
       "rid":"b7c167f6c2da6d93531b9a7b314ad030b3a74803b4b7797edb905ba5a6a08",
       "time-ms":2,
       "cpu-time-ms":0
    }
    } 

它有很多字段,但我只想要数据字段。这不起作用:

It has many fields, but I just have want the Data field. This won't work:

mapper.readvalue(jsonString,Data.class); 

如何让杰克逊只阅读数据字段?

How do I make Jackson read just the "Data" field?

推荐答案

Jackson 2.3现在有一个你可以使用的JsonPointer类。这个版本的快速概述中有一个简单的例子。

Jackson 2.3 now has a JsonPointer class you can use. There's a simple example in their quick overview for the release.


用法很简单:对于JSON,如

Usage is simple: for JSON like

{
    "address" : { "street" : "2940 5th Ave", "zip" : 980021 },   
    "dimensions" : [ 10.0, 20.0, 15.0 ] 
}

您可以使用以下表达式:

you could use expressions like:

  JsonNode root = mapper.readTree(src); 
  int zip =root.at("/address/zip").asIntValue(); 
  double height = root.add("/dimensions/1").asDoubleValue();// assuming it's the second number in there


这篇关于使用Jackson读取JSON字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆