我卡住了。看起来这应该有效,但事实并非如此。我正在尝试访问对象的键值,但它不起作用。我以前做过这个,并且在堆栈溢出和其他地方阅读了很多东西,但它仍然不起作用。这是我的代码: var validate = UrlFetchApp.fetch(url, options); Logger.log('validate = ' + validate); Logger.log("validate['response'] = " + validate["response"]); Logger.log('validate.response = ' + validate.response); Logger.log("validate.['done'] = " + validate["done"]); Logger.log("validate.done = " + validate.done); var validateObj = JSON.stringify(JSON.parse(validate)); Logger.log("validateObj = " + validateObj); Logger.log("validateObj.response = " + validateObj.response); Logger.log("validateObj.done = " + validateObj.done);这是日志:validate = { "done": true, "response": { "@type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse", "result": "Invalid Full Name. Please enter a valid Full Name." }}validate['response'] = undefinedvalidate.response = undefinedvalidate.['done'] = undefinedvalidate.done = undefinedvalidateObj = {"done":true,"response":{"@type":"type.googleapis.com/google.apps.script.v1.ExecutionResponse","result":"Invalid Full Name. Please enter a valid Full Name."}}validateObj.response = undefinedvalidateObj.done = undefined使用括号表示法时,我也尝试过使用单引号和双引号。谁能帮我看看错误?
1 回答
HUX布斯
TA贡献1876条经验 获得超6个赞
您正在尝试从字符串访问responseand属性,因此您得到了. 您已尝试对响应执行操作,但随后返回到字符串,只需执行并访问该属性doneundefinedJSON.parseJSON.stringifyJSON.parse
var validateObj = JSON.parse(validate); // Now you have a deserialized object
Logger.log("validateObj = " + validateObj); // You should get "validateObj = [object Object]"
Logger.log("validateObj.response = " + validateObj.response);
Logger.log("validateObj.done = " + validateObj.done);
Logger.log("validateObj[ 'response' ] = " + validateObj[ 'response' ] );
Logger.log("validateObj[ 'done' ]= " + validateObj[ 'done' ]);
添加回答
举报
0/150
提交
取消