更新:修复了示例代码中的错误。原来我的问题是由var jsonVar = jsonVar.concat(results.records);
如何在循环中连接JSON对象?我可以像这样连接2个JSON对象:
var json1 = {
"records": [{
"id": 28100988,
"work_text_reviews_count": 13,
"average_rating": "3.10"
}, {
"id": 10280687,
"work_text_reviews_count": 80,
"average_rating": "3.87"
}]
}
var json2 = {
"records": [{
"id": 16135639,
"work_text_reviews_count": 0,
"average_rating": "0.00"
}, {
"id": 17978337,
"work_text_reviews_count": 2414,
"average_rating": "3.76"
}, {
"id": 360721218,
"work_text_reviews_count": 4924,
"average_rating": "3.98"
}]
}
var json3 = json1.records.concat(json2.records);
要添加第3个JSON对象,我知道我可以添加.concat(json3.records) 但是如何在循环中动态连接JSON对象呢?
实施例: 假设values.length = 5,这意味着需要连接5个JSON对象。
for (var i=0; i<values.length ; i++) {
response= UrlFetchApp.fetch(url);
Utilities.sleep(1000);
var results = JSON.parse(response);
// this works now (had a typo here)
var jsonVar = jsonVar.concat(results.records);
}
答案 0 :(得分:0)
您可以执行以下操作:
var jsonVar = [];
for (var i=0; i<values.length ; i++) {
results= UrlFetchApp.fetch(url);
Utilities.sleep(1000);
var results = JSON.parse(response);
jsonVar = jsonVar.concat(results.records);
}
但我不确定这是否有效,因为UrlFetchApp.fetch()
似乎是异步的。这意味着如果response
需要超过1000毫秒
require
被初始化为正确值
答案 1 :(得分:-1)
JSON对象是一个Javascript对象。您可以动态设置对象名称
{"Diagnosis":"vvvvv",
"Treatment":[{
"name":"treatment1",
"SubTreatment":[{
"name":"subtreatment1",
"SubTreatment2" : [{
"name":"subtreatmentexp1",
"$$hashKey":"object:27","Name":"vvvvv"
}],
"$$hashKey":"object:22","Name":"vvvvv"
}],
"$$hashKey":"object:12",
"Name":"vvvvv"
}
]}