我使用app脚本并尝试通过API从返回数组中获取值,这是返回数组:
这是完整的JSON:
{data=[{first=[[10, 1], [14, 0], [13, 5]}]}
我使用这段代码来获得值,但不需要修改:
var request = UrlFetchApp.fetch(url);
var json = JSON.parse(request);
// this is the code to get value
const value= json.data.first.map(obj => [obj[0]]);
错误是:TypeError: Cannot read property 'map' of undefined
发布于 2021-09-28 15:17:54
您所包含的完整json数组没有正确关闭。
改为将其修改为
{data=[{first=[[10, 1], [14, 0], [13, 5]]}]}
这样做后,这段代码就可以工作了。
代码:
function myFunction() {
var json = {data: [{first: [[10, 1], [14, 0], [13, 5]]}]};
Logger.log(json)
// access first array element of data
var value= json.data[0].first.map(obj => [obj[0]]);
Logger.log(value)
}
使用数据
使用数据:
发布于 2021-09-28 13:40:16
显示你的反应
但也许只是
const [first] = json.data || []; // get 0 from array
const values = first.map(obj => [obj[0]])
或者可能获得对象属性
const { first } = json.data || {}; // get propetry first
const values = first.map(obj => [obj[0]])
发布于 2021-09-28 14:27:19
在我看来,您的数据不像JSON:
这样做是可行的:
function myfunk() {
const resp = '{"time":1632835594443, "first":[[10, 1], [14, 0.16306, 0], [13, 5]]}';
const obj = JSON.parse(resp);
Logger.log(JSON.stringify(obj.first));
}
8:26:36 AM Notice Execution started
8:26:37 AM Info [[10,1],[14,0.16306,0],[13,5]]
8:26:37 AM Notice Execution completed
https://stackoverflow.com/questions/69362784
复制相似问题