欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jsonpath的helloworld

程序员文章站 2022-05-30 10:13:43
...
基本语法:https://www.cnblogs.com/jpfss/p/10973590.html

这里有个表格,说明JSONPath语法元素和对应XPath元素的对比。
XPath JSONPath Description
/ $ 表示根元素
. @ 当前元素
/ . or [] 子元素
.. n/a 父元素
// .. 递归下降,JSONPath是从E4X借鉴的。
* * 通配符,表示所有的元素
@ n/a 属性访问字符
[] []
子元素操作符
| [,]
连接操作符在XPath 结果合并其它结点集合。JSONP允许name或者数组索引。
n/a [start:end:step]
数组分割操作从ES4借鉴。
[] ?()
应用过滤表示式
n/a ()
脚本表达式,使用在脚本引擎下面。
() n/a Xpath分组



模糊匹配可以用正则:
https://bbs.csdn.net/topics/290047788
or运算用逗号分隔:
https://www.jb51.cc/js/159788.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript JSONPath example | JSON tutorial | w3resource</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://www.w3resource.com/JSON/json.js"></script>
<script type="text/javascript" src="http://www.w3resource.com/JSON/jsonpath.js"></script>
</head>
<body>
<h1>This is an example of JavaScript with JSONPath</h1>
<script type="text/javascript">
               var json = { "MovieDatabase": {
                                            "movie": [ 
                                                       { "name":"nnn",
                                                          "genre": "comedy",
                                                          "director": "teacher",
                                                          "Facebook_like": 252
                                                       },
                                                       {  "name":"helloc",
                                                          "director": "doctor",
                                                          "Facebook_like": 100,
                                                          "movie":{ 
                                                                      "name":"haohao",
                                                                      "genre": "comedy",
                                                                      "director": "killinux",
                                                                      "Facebook_like": 300
                                                                   }
                                                       },
                                                       { "name":"haha",
                                                          "genre": "adventure",
                                                          "director": "Ruben Fleischer",
                                                          "Facebook_like": 114
                                                       }
                                                     ]
                                              }
                          };
               result = "";
               result += jsonPath(json, "$.MovieDatabase.movie[*].director").toJSONString() + "<br />";
               //find all directors
               result += jsonPath(json, "$..director").toJSONString() + "<br />";
               //find all directors
               result += "movie----"+jsonPath(json, "$..movie").toJSONString() + "<br />";
               //find all directors
               result += "*-----"+jsonPath(json, "$.MovieDatabase.*").toJSONString() + "<br />";
               //find all movies
               result += jsonPath(json, "$.MovieDatabase..Facebook_like").toJSONString() + "<br />";
               //find all facebook lies of all the movies
               result += jsonPath(json, "$..movie[(@.length-1)]").toJSONString() + "<br />";
               //the last movie in data
               result += jsonPath(json, "$..movie[-1:]").toJSONString() + "<br />";
               //the last movie in data
               result += jsonPath(json, "$..movie[0,1]").toJSONString() + "<br />";
               //first two movies
               result += jsonPath(json, "$..movie[:3]").toJSONString() + "<br />";
               //first three movies
               result += "genre---"+jsonPath(json, "$..movie[?(@.genre)]").toJSONString() + "<br />";
               //or movies with genre
               result +="Facebook_like----"+ jsonPath(json, "$..movie[?(@.Facebook_like>200)]").toJSONString() + "<br />";
               // condition
               result += "or----"+jsonPath(json, "$..[?(@.name == 'nnn'),?(@.name == 'haohao')]").toJSONString() + "<br />";
               //or
               result += "*----"+jsonPath(json, "$..[?(/ha/.test(@.name))]]").toJSONString() + "<br />";
               //fuzzy
               result += "all----"+jsonPath(json, "$..*").toJSONString() + "\n";
               // all members in the JSON document
               document.write(result);
  </script>
  </body>
  </html>

相关标签: javascript