简体   繁体   English

如何从XMLHttpRequest解析javascript中的该值并将其用作变量?

[英]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.

相关问题 在 JavaScript 中,我/应该如何将 async/await 与 XMLHttpRequest 一起使用? - In JavaScript how do I/should I use async/await with XMLHttpRequest? 我如何使用 JavaScript 将输入从文本框中获取到变量然后打印变量的值? - How do i use JavaScript to take the input from a text box to a variable then print the value of a variable? 如何使用 drupal 视图字段值作为 Javascript 变量 - How do I use a drupal views field value as Javascript Variable 我如何在JavaScript中使用asp.net变量值 - How do i use an asp.net variable value in javascript 如何使用 javascript 解析值? - How do I parse a value using javascript? 如何在javascript中循环遍历xmlhttprequest - How do I loop through an xmlhttprequest in javascript 我如何使用 javascript 将值从变量传递给 .html() - How do i pass the value from a variable to .html() using javascript 如何读取 XMLHttpRequest.onreadystatechange 回调函数内的 FileReader.onloadend 中定义的变量的值? - How do I read value of variable defined in FileReader.onloadend inside XMLHttpRequest.onreadystatechange callback functin? 我是否需要 XMLHttpRequest 来使用 JavaScript 更改 JSON 文件值? - Do I need an XMLHttpRequest to change a JSON file value with JavaScript? 如何使用JavaScript中的XMLHttpRequest更改“ Cookie”(在标头字段中)中一个cookie的值? - How do I change the value of one cookie in 'Cookie' (in the header field) with XMLHttpRequest in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM