过滤从 UrlFetchApp 接收到的 JSON 数据
Filtering the JSON data which is received from UrlFetchApp
在脚本中,我使用 UrlFetchApp.fetch
从 API 的 URL 导入 JSON 数据
接下来看这部分代码:
var jsondata = UrlFetchApp.fetch(url, fetchOptions);
这里是我使用的 JSON 的简短示例:
[
{
"average": 5.76,
"date": "2021-03-16",
"highest": 5.88,
"lowest": 5.41,
"order_count": 2214,
"volume": 5093879501
},
{
"average": 5.88,
"date": "2021-03-17",
"highest": 5.92,
"lowest": 5.46,
"order_count": 2285,
"volume": 3968980696
},
{
"average": 5.68,
"date": "2021-03-18",
"highest": 5.91,
"lowest": 5.6,
"order_count": 2219,
"volume": 3695305537
},
{
"average": 5.65,
"date": "2021-03-19",
"highest": 5.98,
"lowest": 5.59,
"order_count": 2192,
"volume": 3948557942
}
]
我需要在脚本中进一步处理上述数据之前对其进行过滤,我的问题是我将此 JSON 数据作为 jsondata
对象获取,没有实际的 JSON代码。
我认为过滤数据最方便的方法是将 jsondata
转换为数组(以便使用 .slice 和 .map 用于我的过滤),然后将结果数组转换回对象以在脚本中进一步使用,但我不知道该怎么做。
我相信您得到的是 JSON 字符串,您需要使用函数 JSON.parse() 将其转换为对象。
所以,使用这个:
let jsondata = JSON.parse(UrlFetchApp.fetch(url, fetchOptions));
JSON.parse 是一个安全函数,用于解析 JSON 字符串并将它们转换为对象。
现在您将拥有一个可以用它做任何事情的对象。
With an object you can use many JS native functions as:
Object.keys(obj) to extract the keys
Object.values(obj) to extract the values
Object.entries(obj) to extract both
And you can use direct filter to do what you want.
Example:
const array = [your array here]
const filterarray = array.filter((item) => item.highest > 5.9))
console.log(filterarray)
答案:您只会返回 3 个高于 5.9 的数组。
(3)[{...},{...},{...}
如果您想将数组转换为字符串,您也可以使用 JSON.stringify。
希望能帮到你!
干杯,
马塞洛
function jtoa() {
const json = '[{"average":5.76,"date":"2021-03-16","highest":5.88,"lowest":5.41,"order_count":2214,"volume":5093879501},{"average":5.88,"date":"2021-03-17","highest":5.92,"lowest":5.46,"order_count":2285,"volume":3968980696},{"average":5.68,"date":"2021-03-18","highest":5.91,"lowest":5.6,"order_count":2219,"volume":3695305537},{"average":5.65,"date":"2021-03-19","highest":5.98,"lowest":5.59,"order_count":2192,"volume":3948557942}]';
let obj = JSON.parse(json);
let a = obj.map(o => {return [o.average, o.date, o.highest, o.lowest, o.order_count, o.volume];} );
Logger.log(JSON.stringify(a));
}
因此,您总是会从上述任何 http 请求中获得一个 JSON 字符串对象。
然后你需要解析它:
let object = JSON.parse(UrlFetchApp.fetch(url, fetchOptions));
一旦你有了你的对象,你就可以做任何你想做的事。
因此,您可以使用析构函数 (...) 运算符将此对象复制到另一个对象:
let object2 = [ ...object ]
在这里,对象 2 是对象 1 的精确副本。
过滤器命令作为一个副本工作,但 returns 只是与句子匹配的值。如我上面的回答所示。
如果你想 return 给你一些项目最高高于 5.9 的情况,你可以使用方法:map 和 filter 一起使用:
案例 1:
const json = '[{"average":5.76,"date":"2021-03-16","highest":5.88,"lowest":5.41,"order_count":2214,"volume":5093879501},{"average":5.88,"date":"2021-03-17","highest":5.92,"lowest":5.46,"order_count":2285,"volume":3968980696},{"average":5.68,"date":"2021-03-18","highest":5.91,"lowest":5.6,"order_count":2219,"volume":3695305537},{"average":5.65,"date":"2021-03-19","highest":5.98,"lowest":5.59,"order_count":2192,"volume":3948557942}]';
let obj = JSON.parse(json);
const obj2 = obj.map(obj => obj.highest).filter((item) => item > 5.9).length;
它将return数字3。
如果您希望答案是一个数组,您只需删除 length:
const obj2 = obj.map(obj => obj.highest).filter((item) => item > 5.9);
它将 return: [5.92, 5.91, 5.98]
所以,这些只是一些例子。大家可以上网看看。
干杯,
马塞洛
在脚本中,我使用 UrlFetchApp.fetch
接下来看这部分代码:
var jsondata = UrlFetchApp.fetch(url, fetchOptions);
这里是我使用的 JSON 的简短示例:
[
{
"average": 5.76,
"date": "2021-03-16",
"highest": 5.88,
"lowest": 5.41,
"order_count": 2214,
"volume": 5093879501
},
{
"average": 5.88,
"date": "2021-03-17",
"highest": 5.92,
"lowest": 5.46,
"order_count": 2285,
"volume": 3968980696
},
{
"average": 5.68,
"date": "2021-03-18",
"highest": 5.91,
"lowest": 5.6,
"order_count": 2219,
"volume": 3695305537
},
{
"average": 5.65,
"date": "2021-03-19",
"highest": 5.98,
"lowest": 5.59,
"order_count": 2192,
"volume": 3948557942
}
]
我需要在脚本中进一步处理上述数据之前对其进行过滤,我的问题是我将此 JSON 数据作为 jsondata
对象获取,没有实际的 JSON代码。
我认为过滤数据最方便的方法是将 jsondata
转换为数组(以便使用 .slice 和 .map 用于我的过滤),然后将结果数组转换回对象以在脚本中进一步使用,但我不知道该怎么做。
我相信您得到的是 JSON 字符串,您需要使用函数 JSON.parse() 将其转换为对象。
所以,使用这个:
let jsondata = JSON.parse(UrlFetchApp.fetch(url, fetchOptions));
JSON.parse 是一个安全函数,用于解析 JSON 字符串并将它们转换为对象。 现在您将拥有一个可以用它做任何事情的对象。
With an object you can use many JS native functions as:
Object.keys(obj) to extract the keys
Object.values(obj) to extract the values
Object.entries(obj) to extract both
And you can use direct filter to do what you want.
Example:
const array = [your array here]
const filterarray = array.filter((item) => item.highest > 5.9))
console.log(filterarray)
答案:您只会返回 3 个高于 5.9 的数组。
(3)[{...},{...},{...}
如果您想将数组转换为字符串,您也可以使用 JSON.stringify。
希望能帮到你!
干杯, 马塞洛
function jtoa() {
const json = '[{"average":5.76,"date":"2021-03-16","highest":5.88,"lowest":5.41,"order_count":2214,"volume":5093879501},{"average":5.88,"date":"2021-03-17","highest":5.92,"lowest":5.46,"order_count":2285,"volume":3968980696},{"average":5.68,"date":"2021-03-18","highest":5.91,"lowest":5.6,"order_count":2219,"volume":3695305537},{"average":5.65,"date":"2021-03-19","highest":5.98,"lowest":5.59,"order_count":2192,"volume":3948557942}]';
let obj = JSON.parse(json);
let a = obj.map(o => {return [o.average, o.date, o.highest, o.lowest, o.order_count, o.volume];} );
Logger.log(JSON.stringify(a));
}
因此,您总是会从上述任何 http 请求中获得一个 JSON 字符串对象。 然后你需要解析它:
let object = JSON.parse(UrlFetchApp.fetch(url, fetchOptions));
一旦你有了你的对象,你就可以做任何你想做的事。 因此,您可以使用析构函数 (...) 运算符将此对象复制到另一个对象:
let object2 = [ ...object ]
在这里,对象 2 是对象 1 的精确副本。 过滤器命令作为一个副本工作,但 returns 只是与句子匹配的值。如我上面的回答所示。
如果你想 return 给你一些项目最高高于 5.9 的情况,你可以使用方法:map 和 filter 一起使用: 案例 1:
const json = '[{"average":5.76,"date":"2021-03-16","highest":5.88,"lowest":5.41,"order_count":2214,"volume":5093879501},{"average":5.88,"date":"2021-03-17","highest":5.92,"lowest":5.46,"order_count":2285,"volume":3968980696},{"average":5.68,"date":"2021-03-18","highest":5.91,"lowest":5.6,"order_count":2219,"volume":3695305537},{"average":5.65,"date":"2021-03-19","highest":5.98,"lowest":5.59,"order_count":2192,"volume":3948557942}]';
let obj = JSON.parse(json);
const obj2 = obj.map(obj => obj.highest).filter((item) => item > 5.9).length;
它将return数字3。 如果您希望答案是一个数组,您只需删除 length:
const obj2 = obj.map(obj => obj.highest).filter((item) => item > 5.9);
它将 return: [5.92, 5.91, 5.98]
所以,这些只是一些例子。大家可以上网看看。
干杯, 马塞洛