[英]How do I parse this value in javascript from XMLHttpRequest and use it as a variable?
I am working on a chrome extension that gets some variables from a google sheets spreadsheet. 我正在开发一个Chrome扩展程序,该扩展程序从Google表格电子表格中获取了一些变量。 I finally got the api setup correctly and can successfully query the data for the first variable from a cell in that spreadsheet. 我终于正确设置了api,可以成功地从该电子表格中的单元格中查询数据以获取第一个变量。 The problem for me is I am not familiar with the format the data is in. 我的问题是我不熟悉数据的格式。
Here is my code to query the google spreadsheet: 这是我查询Google电子表格的代码:
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://sheets.googleapis.com/v4/spreadsheets/*spreadsheetID*/values/A2?key=*API Key*", false);
xhr.send();
var result = xhr.responseText;
The output I receive is: 我收到的输出是:
{ "range": "Sheet1!A2", "majorDimension": "ROWS", "values": [ [ "https://www.access.com/home/" ] ] }
the URL https://www.access.com/home is what I am trying to parse from the spreadsheet. URL https://www.access.com/home是我试图从电子表格中解析的内容。 If I change the code to parse the responseText in json I can get the value for range, just not for the "values" data to get the URL: 如果我更改代码以解析json中的responseText,则可以获取range的值,而不能获取“ values”数据的URL:
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://sheets.googleapis.com/v4/spreadsheets/*spreadsheetID*/values/A2?key=*API Key*", false);
xhr.send();
var result = JSON.parse(xhr.responseText);
var data = result.range;
What am I doing wrong and how do I correctly parse the URL? 我在做错什么,如何正确解析URL?
*While the post at Access / process (nested) objects, arrays or JSON does a great job at explaining how to access nested objects that are named, I was unable to figure out that how to access the nested array in my object because it didn't have a name. *虽然Access / process(嵌套)对象,数组或JSON的帖子在解释如何访问命名的嵌套对象方面做得很好,但我无法弄清楚如何访问对象中的嵌套数组,因为它没有没有名字
var data = result.values[0][0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.