JsonPath 介绍

 

什么是 JsonPath

 

JsonPath JSON 的关系,正如 XPath XML 的关系一样。JsonPath 设计来作为 JSON 的路径语言,用于确定 JSON 文档中某部分位置的语言。

 

JsonPath 原理

 

JsonPath 将 JSON 数据转换为 DOM 树状结构,并提供在数据结构树种寻找节点的能力。 

 
JsonPath 介绍
 

 

JsonPath语法

 

JsonPath 使用路径表达式在 JSON文档(字符串)中选取节点,节点是通过沿着路径来选取的。路径由节点名组成,节点之间以"."分割,且路径必须是由根节点开始的完全绝对路径。下面以例子说明:

 

测试 JSON 数据

 

{

 

  "store":

 

  {

 

    "book":

 

    [

 

      {

 

        "category": "reference",

 

        "author": "Nigel",

 

        "title": "Sayings of the Century",

 

        "price": 8.95

 

      },

 

      {

 

        "category": "fiction",

 

        "author": "Evelyn",

 

        "title": "Sword of Honour",

 

        "price": 12.99

 

      }

 

    ],

 

    "bicycle":

 

    {

 

      "color": "red",

 

      "band":"吉安特",

 

      "price": 19.95

 

    }

 

  },

 

  "expensive": 10

 

}

 

1.     选取 bicycle band

 

o    Path: store.bicycle.band

 

o    返回: "吉安特"

 

2.     选取 book 数组下的所有author

 

o    Path: store.book[*].author

 

o    :必须通过[]显示指定某个节点类型为数组。

 

o    返回: ["Nigel","Evelyn"]

 

3.     条件表达式 JsonPath 支持通过简单的条件表达式(目前仅支持相等条件判断)来筛选节点,条件表达式须要放置在中括号中[]。如,选取 book author Nigel price :

 

o    Path: store.book[author="Nigel"].price

 

o    返回: "8.95"

 

 

 

 

相关文章:

  • 2022-12-23
  • 2021-07-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2022-01-12
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2021-06-19
相关资源
相似解决方案