Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 从应用程序脚本UrlFetchApp.fetch命令返回的问题分析字典_Json_Parsing_Google Apps Script_Http Headers_Http Post - Fatal编程技术网

Json 从应用程序脚本UrlFetchApp.fetch命令返回的问题分析字典

Json 从应用程序脚本UrlFetchApp.fetch命令返回的问题分析字典,json,parsing,google-apps-script,http-headers,http-post,Json,Parsing,Google Apps Script,Http Headers,Http Post,解析通过简单API调用从UrlFetchApp.fetch返回的字典时出现问题 代码如下: var HTTPResponse = UrlFetchApp.fetch('<myURL>', options); HTTPResponse from返回如下内容: var results = JSON.parse(HTTPResponse); {"Category 1": "Value 1", "Category 2": "Value 2", "Category 3": "Value

解析通过简单API调用从UrlFetchApp.fetch返回的字典时出现问题

代码如下:

var HTTPResponse = UrlFetchApp.fetch('<myURL>', options); 
HTTPResponse from返回如下内容:

var results = JSON.parse(HTTPResponse);
{"Category 1": "Value 1", "Category 2": "Value 2", 
"Category 3": "Value 3", "Processing Team": "Processing Team 1”}
然后,当我这样做时:results[“Category 1”]我得到“undefined”,因为暗示“Category 1”的值不是字典中的项目

API调用返回的对象或内容类型导致无法解析该字典。可以在Python解释器中解析相同的词典

我错过了什么?看起来很明显


谢谢你的提示

您不应该解析返回的
HTTPResponse

您应该做的第一件事是检查响应代码是否为200,然后将内容获取为文本,然后将文本解析为JSON。
HTTPResponse
实际上不是返回的有效负载。使用HTTPResponse类的
getContentText()
方法检索返回

var HTTPResponse = UrlFetchApp.fetch('<myURL>', options);

if (HTTPResponse.getResponseCode() !== 200) {
  throw new Error("Request Failed: " + HTTPResponse.getContentText());
  return;
}

var results = HTTPResponse.getContentText();

results = JSON.parse(results);
var-HTTPResponse=UrlFetchApp.fetch(“”,选项);
如果(HTTPResponse.getResponseCode()!==200){
抛出新错误(“请求失败:+HTTPResponse.getContentText());
返回;
}
var results=HTTPResponse.getContentText();
results=JSON.parse(results);

HTTPResponse
不是JavaScript-JSON。它是一个对象,如果使用
typeof
测试数据类型,它将作为一个对象进行计算,但它不能正确解析。当您尝试解析HTTPResponse时,您尝试解析的对象已经是一个对象,而不是JavaScript对象,而是一个应用程序脚本类。

欢迎使用StackOverFlow,请借此机会学习,并了解如何使用,以及。谢谢你,艾伦。最后的挑战。。。将对象转换为文本,然后将文本转换为JSON对象(如您所建议的),将返回以下结果值:[{Category 2=Category 2,Processing Team=Processing Team 1,Category 1=Category 1,Category 3=Category 3}]。使用结果[“Category 1”]执行查找时返回null(可能是因为冒号已被等号替换?)。再一次,我知道这应该是显而易见的。我如何解析用大括号和方括号包装的返回字典,其中键和值用等号分隔?帮助业余爱好者的好业力:)。请尝试
results[0][“Category 1”]
,因为它看起来像是将对象包装在一个数组中。外部有括号,表示外部对象是数组。谢谢Alan。有趣的是,我之前在解析UrlFetchApp.fetch调用的输出时尝试过这一点,正如您所指出的,该调用返回一个无法解析的对象。现在工作。非常感谢。谢谢你让我知道。