如何根据键的过滤器获取 json 路径?
How to get the json path based on filter on the key?
我有一个 json,我必须 select 来自 json 对象的作者姓名,该对象不包含 available 作为键入它
{
"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
"available":false
},
{
"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
}
},
"expensive": 10
}
所以如果可以使用 jsonpath 是否可以,我该如何实现?
您可以过滤没有 属性 'available'
的书,然后通过过滤得到作者姓名
const books = [
{
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,
available: false
},
{
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
}
]
const authorNames = books
.filter(book => !book.hasOwnProperty('available'))
.map(book => book.author)
console.log(authorNames)
参考
我认为您需要可以为您完成任务的 JSONpath,因此您可以试试这个。
$..book[?(!@.available)].author
试穿
https://jsonpath.com/它的工作方式和你想要的一样
您可以使用 for..in 构造来遍历对象的任意属性:
for (var key in obj.d) {
console.log("Key: " + key);
console.log("Value: " + obj.d[key]);}
或者像这样检查您的可用密钥,例如
var Data_Array = {
"Private": {
"Price": {
"Adult": "18",
"Child": [{
"FromAge": "0",
"ToAge": "12",
"Price": "10"
}]
}
}};var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child';console.log(child);
这会告诉你是否有可用的密钥,然后像这样获取作者姓名,一个例子
通过使用单词“b”,您仍在使用键名。
var info = {"fname": "A","lname": "B","Age": "34","favcolor": {"color1":"Gray", "color2":"Black", "color3":"Blue"}};
请看下面的代码片段。
for(key in info) {var infoJSON = info[key];console.log(infoJSON);}
我有一个 json,我必须 select 来自 json 对象的作者姓名,该对象不包含 available 作为键入它
{
"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
"available":false
},
{
"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
}
},
"expensive": 10
}
所以如果可以使用 jsonpath 是否可以,我该如何实现?
您可以过滤没有 属性 'available'
的书,然后通过过滤得到作者姓名
const books = [
{
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,
available: false
},
{
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
}
]
const authorNames = books
.filter(book => !book.hasOwnProperty('available'))
.map(book => book.author)
console.log(authorNames)
参考
我认为您需要可以为您完成任务的 JSONpath,因此您可以试试这个。
$..book[?(!@.available)].author
试穿 https://jsonpath.com/它的工作方式和你想要的一样
您可以使用 for..in 构造来遍历对象的任意属性:
for (var key in obj.d) {
console.log("Key: " + key);
console.log("Value: " + obj.d[key]);}
或者像这样检查您的可用密钥,例如
var Data_Array = {
"Private": {
"Price": {
"Adult": "18",
"Child": [{
"FromAge": "0",
"ToAge": "12",
"Price": "10"
}]
}
}};var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child';console.log(child);
这会告诉你是否有可用的密钥,然后像这样获取作者姓名,一个例子
通过使用单词“b”,您仍在使用键名。
var info = {"fname": "A","lname": "B","Age": "34","favcolor": {"color1":"Gray", "color2":"Black", "color3":"Blue"}};
请看下面的代码片段。
for(key in info) {var infoJSON = info[key];console.log(infoJSON);}