我一直试图通过以下代码在Google Apps脚本中将JSON响应插入到Google工作表中,但由于某种原因,我在尝试运行时遇到错误。
请查看截图及以下代码。
function myFunction() {
var key_67 = 'YYYYYYYYYYYYYYYYYY';
var ss_67 = SpreadsheetApp.openById(key_67);
var sheet_67 = ss_67.getActiveSheet();
sheet_67.getRange('A1:AZ10000').clearContent();
var url = 'https://creator.zoho.com/api/json/arfater/view/Leads_Report?authtoken=XXXXXXXXXXXXXXXXXXXX&scope=creatorapi&zc_ownername=ipekuet';
var response = UrlFetchApp.fetch(url);
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data);
var stats=[]; //create empty array to hold data points
//The following lines push the parsed json into empty stats array
stats.push(data.Yearly_Sales); //temp
stats.push(data.Email); //dewPoint
stats.push(data.Phone); //visibility
//append the stats array to the active sheet
sheet_67.appendRow(stats)
}
答案 0 :(得分:1)
所以基于邮递员应用程序的JSON响应是
var zohoipekuetview65 = {"Leads":[{"Yearly_Sales":"$ 1,000.00","Email":"test@zoho.com","Phone":"123-032-03323","Potentially":50,"State":"NY","ZipCode":"10036","Street":"1515 Broadway","Country":"USA","ID":"2198633000000063029","City":"New York","Name":"Arfater Rahman"}]};
当我按原样使用该响应时:
function JsonResponse(){
var json ='var zohoipekuetview65 = {"Leads":[{"Yearly_Sales":"$ 1,000.00","Email":"test@zoho.com","Phone":"123-032-03323","Potentially":50,"State":"NY","ZipCode":"10036","Street":"1515 Broadway","Country":"USA","ID":"2198633000000063029","City":"New York","Name":"Arfater Rahman"}]} '
var data = JSON.parse(json);
Logger.log(data);
}
我得到了和你一样的错误:
SyntaxError:意外的令牌:v
这让我相信您对API的回复有这个术语var zohoipekuetview65
(不确定为什么?也许是一个错误)
以下代码将响应字符串拆分为仅提供JSON响应
function trialParse(){
var json ='var zohoipekuetview65 = {"Leads":[{"Yearly_Sales":"$ 1,000.00","Email":"test@zoho.com","Phone":"123-032-03323","Potentially":50,"State":"NY","ZipCode":"10036","Street":"1515 Broadway","Country":"USA","ID":"2198633000000063029","City":"New York","Name":"Arfater Rahman"}]} '
Logger.log(JsonResponse(json))
}
function JsonResponse(response){
Logger.log(response)
var json = response.split("=")[1]
var data = JSON.parse(json);
Logger.log(data);
return data
}
只需使用var data = JsonResponse(json)
最后注意:如Jordan Rhea所述,您可以使用Logger.log(json)
输出对日志的响应。要查看日志,请转到视图>日志,它会显示您从Api收到的回复。