Javascript 我能';不要在谷歌表单上操作JSON
此代码在Javascript中工作:Javascript 我能';不要在谷歌表单上操作JSON,javascript,json,google-apps-script,google-sheets,Javascript,Json,Google Apps Script,Google Sheets,此代码在Javascript中工作: var obj = { "Global Quote": { "01. symbol": "BMGB4.SAO", "02. open": "4.4600", "03. high": "4.6700", "04. low": "4.3000", "05. price": "4.4800", "06. volume": "969900", "0
var obj = {
"Global Quote": {
"01. symbol": "BMGB4.SAO",
"02. open": "4.4600",
"03. high": "4.6700",
"04. low": "4.3000",
"05. price": "4.4800",
"06. volume": "969900",
"07. latest trading day": "2020-04-09",
"08. previous close": "4.4000",
"09. change": "0.0800",
"10. change percent": "1.8182%"
}
};
var myJSON = JSON.stringify(obj);
console.log(myJSON);
myJSON = JSON.parse(myJSON);
console.log(myJSON);
console.log(myJSON["Global Quote"]);
console.log(myJSON["Global Quote"]["05. price"]);
然而,使用Google Sheets,编译器指责我犯了一个错误:TypeError:cannotreadproperty'05。未定义的价格(第53行,文件“代码”)
见鬼,你怎么能在Javascript和Google表单中正常识别JSON
//我隐藏了变量url,但它将返回与我在这里发布的javascript代码相同的JSON
var response = UrlFetchApp.fetch (url);
var data = JSON.parse (JSON.stringify (response.getContentText ()));
Here is my code on Google Sheets:
// We convert it to JSON.
var myJSON = JSON.stringify (data);
Logger.log (myJSON);
myJSON = JSON.parse (myJSON);
Logger.log (myJSON);
Logger.log (myJSON ["Global Quote"]);
Logger.log (myJSON ["Global Quote"]["05. Price"]);
问题:
var data=JSON.parse(JSON.stringify(response.getContentText())
字符串化和解析将产生相同的对象/字符串。这和写作一样
var data=response.getContentText()
数据
现在是字符串
类型
以下事件链导致myJSON
成为字符串
,而不是对象
var myJSON = JSON.stringify (data);// Double stringified
myJSON = JSON.parse (myJSON);//Single stringified
Logger.log (myJSON ["Global Quote"]);//undefined
Logger.log (myJSON ["Global Quote"]["05. Price"]);//error
TypeError:无法读取属性“05”。未定义的价格(第53行,文件“代码”)
解决方案:
parse
将其再次解析或parse
将其直接解析为对象:
const data = response.getContentText(); //string
const yourJSON = JSON.parse(data);//object
console.log (yourJSON ["Global Quote"]);//object
console.log (myJSON ["Global Quote"]["05. Price"]);// no error
问题:
var data=JSON.parse(JSON.stringify(response.getContentText())
字符串化和解析将产生相同的对象/字符串。这和写作一样
var data=response.getContentText()
数据
现在是字符串
类型
以下事件链导致myJSON
成为字符串
,而不是对象
var myJSON = JSON.stringify (data);// Double stringified
myJSON = JSON.parse (myJSON);//Single stringified
Logger.log (myJSON ["Global Quote"]);//undefined
Logger.log (myJSON ["Global Quote"]["05. Price"]);//error
TypeError:无法读取属性“05”。未定义的价格(第53行,文件“代码”)
解决方案:
parse
将其再次解析或parse
将其直接解析为对象:
const data = response.getContentText(); //string
const yourJSON = JSON.parse(data);//object
console.log (yourJSON ["Global Quote"]);//object
console.log (myJSON ["Global Quote"]["05. Price"]);// no error
stringify和parse链是什么?stringify和parse链是什么?在google sheats中,510/5000可以使用以下语法:myJSON[“Global Price”][“05.Price”]);他不接受我之前做过的[“05.Price”]参数,它使用的是另一个相同格式的Json,但这个结果不起作用,奇怪的是,在这个参数中,我从另一个URL获得的,它使用的是完全相同的参数。我不知道当我更改URL时,他怎么会觉得这个参数很奇怪。我设法用以下代码绕过了这种情况:函数callApi(url,posicaolinha,posicaocoluna){var response=urlfetchap.fetch(url);//Logger.log(response.getContentText());//Evitamos que aconteça qualquer BUG louco JSON vindo da url,resultara em uma String formatada corretatione.var data=JSON.parse(JSON.stringify(response.getContentText());//convertedmos em JSON.var myJSON=JSON.stringify(data);//Tentamos converter字符串em Objeto.myJSON=JSON.parse(myJSON);//Nos certificamos de functionar a conversãde String para Objeto.var convertidoEmObjeto=JSON.parse(myJSON);var Objeto=Object.values(convertidemobjeto);//acessamas o preço da açço,pelo objeto.var valorAtualAcao=objeto[0][“05.price”];//convertermos o preço da aço da aço em um Float.valorAtualAcao=parseFloat(valorAtualAcao);var sheet=SpreadsheetApp.getActiveSheet();//Linha/Coluna sheet.getRange(posicaolinha,posicaocoluna).setValue([valorAtualAcao]);)我不得不强制转换。JSON.parse函数中有一个bug没有将字符串转换为Obj,所以我不得不叫她2x。@正如我在回答中所解释的,这不是bug。您将它字符串化了两次……所以您必须对它进行两次解析。Do
const data=response.getContentText();//string const myJSON=JSON.parse(data);//object console.log(myJSON[“Global Quote”]);//object console.log(myJSON[“Global Quote”][“05.Price”]);//没有错误
510/5000在google sheats中,您可以使用以下语法:myJSON[“Global Price”][“05.Price”]);他不接受我之前做过的[“05.Price”]参数,它使用的是另一个相同格式的Json,但这个结果不起作用,奇怪的是,在这个参数中,我从另一个URL获得的,它使用的是完全相同的参数。我不知道当我更改URL时,他怎么会觉得这个参数很奇怪。我设法用以下代码绕过了这种情况:函数callApi(url,posicaolinha,posicaocoluna){var response=urlfetchap.fetch(url);//Logger.log(response.getContentText());//Evitamos que aconteça qualquer BUG louco JSON vindo da url,resultara em uma String formatada corretatione.var data=JSON.parse(JSON.stringify(response.getContentText());//convertedmos em JSON.var myJSON=JSON.stringify(data);//Tentamos converter字符串em Objeto.myJSON=JSON.parse(myJSON);//Nos certificamos de functionar a conversãde String para Objeto.var convertidoEmObjeto=JSON.parse(myJSON);var Objeto=Object.values(convertidemobjeto);//acessamas o preço da açço,pelo objeto.var valorAtualAcao=objeto[0][“05.price”];//convertermos o preço da aço da aço em um Float.valorAtualAcao=parseFloat(valorAtualAcao);var sheet=SpreadsheetApp.getActiveSheet();//Linha/Coluna sheet.getRange(posicaolinha,posicaocoluna).setValue([valorAtualAcao]);)我不得不强制转换。JSON.parse函数中有一个bug没有将字符串转换为Obj,所以我不得不叫她2x。@正如我在回答中所解释的,这不是bug。您将它字符串化了两次……所以您必须对它进行两次解析。Doconst data=response.getContentText();//string const myJSON=JSON.parse(data);//object console.log(myJSON[“Global Quote”]);//object console.log(myJSON[“Global Quote”][“05.Price”]);//无错误