我正在使用Google Script获取一个返回以下JSON对象的网址:
[
{
"rsid":"op-bigideas",
"site_title":"Big Ideas",
"evars":[
{
"name":"Tracking Code",
"type":"text_string",
"id":"trackingcode",
"expiration_type":"week",
"expiration_custom_days":"1",
"allocation_type":"most_recent_last"
},
{
"name":"Custom eVar 1",
"description":"",
"type":"text_string",
"enabled":false,
"id":"evar1",
"expiration_type":"visit",
"expiration_custom_days":1,
"allocation_type":"most_recent_last"
}
]
}
]
如何使用name
使用evars
从javascript
中提取Google Apps Script
属性?
这是返回JSON对象的代码:
var elements = JSON.parse(UrlFetchApp.fetch(url, options));
我已尝试过以下操作,但只收到undefined
消息:
1
for(var elem in elements) {
Logger.log(elements[elem]['evars'].name);
}
2
for(var elem in elements) {
Logger.log(elements[elem].evars.name);
}
3
var newData = JSON.parse(elements);
Logger.log(newData.evars.name)
答案 0 :(得分:1)
如果我理解正确,您希望获取name
属性的值。此代码将记录name
属性:
for (var elem in elements) {
for (var evar in elements[elem].evars) {
Logger.log(elements[elem].evars[evar].name);
}
}
这将输出:
"Tracking Code"
"Custom eVar 1"
答案 1 :(得分:0)
每个元素都有多个name
属性,因此我认为您要映射两次:
elements.map(function(elt) {
return !elt.evars ? [] : elt.evars.map(function(evar) {
return evar.name;
});
});
这将为您提供一组数组;外部数组是元素,每个元素都包含一个名称数组。对于您的示例,这将是:
[
["Tracking Code", "Custom eVar 1"]
]
答案 2 :(得分:0)
for (var elem in elements[0].evars) {
Logger.log(elements[0].evars[elem].name);
}