Javascript使用正则表达式

时间:2015-06-16 02:27:25

标签: javascript regex json object nested

我有这个:

var foo = {
          "Category1": [
            {"Company1": {"URL": ["DomainName1", "DomainName2"]}},
            ...
          ],
          ...
          }

通常,我会像这样访问DomainName1:

foo["Category1"][0]["Company1"]["URL"][0]

但是,我想搜索所有foo以查找某个DomainName,我不知道任何其他信息。我知道我可以使用几个嵌套的for循环,但那是非常非常慢。有效的方法是什么?我在想'*'代替["Category1"][0]等等,但我不知道该怎么做。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

使用Jsonpath解决此问题是一个很好的问题。

例如,您可以使用此表达式查找所有URL:

var out = jsonPath(json, "$..URL[*]").toJSONString() + "\n";
document.write(out);

您可以使用表达式匹配所需的域。

对于您的情况,您可以使用以下表达式:

$..URL[?(@.indexOf('DomainName1') != -1)]

<强> Jsonpath online tool

以下是 documentation 中有用的Jsonpath示例:

<html>
<head>
<title> JSONPath - Example (js)</title>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript" src="jsonpath.js"></script>
</head>
<body>
<pre>
<script type="text/javascript">
   var json = 
                  { "store": {
                        "book": [ 
                          { "category": "reference",
                                "author": "Nigel Rees",
                                "title": "Sayings of the Century",
                                "price": 8.95
                          },
                          { "category": "fiction",
                                "author": "Evelyn Waugh",
                                "title": "Sword of Honour",
                                "price": 12.99
                          },
                          { "category": "fiction",
                                "author": "Herman Melville",
                                "title": "Moby Dick",
                                "isbn": "0-553-21311-3",
                                "price": 8.99
                          },
                          { "category": "fiction",
                                "author": "J. R. R. Tolkien",
                                "title": "The Lord of the Rings",
                                "isbn": "0-395-19395-8",
                                "price": 22.99
                          }
                        ],
                        "bicycle": {
                          "color": "red",
                          "price": 19.95
                        }
                  }
                },
           out = "";
       out += jsonPath(json, "$.store.book[*].author").toJSONString() + "\n>";
       out += jsonPath(json, "$..author").toJSONString() + "\n";
       out += jsonPath(json, "$.store.*").toJSONString() + "\n";
       out += jsonPath(json, "$.store..price").toJSONString() + "\n";
       out += jsonPath(json, "$..book[(@.length-1)]").toJSONString() + "\n";
       out += jsonPath(json, "$..book[-1:]").toJSONString() + "\n";
       out += jsonPath(json, "$..book[0,1]").toJSONString() + "\n";
       out += jsonPath(json, "$..book[:2]").toJSONString() + "\n";
       out += jsonPath(json, "$..book[?(@.isbn)]").toJSONString() + "\n";
       out += jsonPath(json, "$..book[?(@.price<10)]").toJSONString() + "\n";
       out += jsonPath(json, "$..*").toJSONString() + "\n";
       document.write(out);
  </script>
</pre>
</body>
</html>

示例输出:

["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
[[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95}]
[8.95,12.99,8.99,22.99,19.95]
[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}]
[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}]
[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]
[{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}},[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95},{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99},"reference","Nigel Rees","Sayings of the Century",8.95,"fiction","Evelyn Waugh","Sword of Honour",12.99,"fiction","Herman Melville","Moby Dick","0-553-21311-3",8.99,"fiction","J. R. R. Tolkien","The Lord of the Rings","0-395-19395-8",22.99,"red",19.95]

只需使用您想要的表达式轻松查询您的json结构。

答案 1 :(得分:0)

我的回答可能仍然是固执己见的......对于成千上万的对象,你试图重新发明轮子。这是数据库存储的精确候选者。 SQL或非SQL,如MongoDB。