访问 JSON 对象的键值

我卡住了。看起来这应该有效,但事实并非如此。


我正在尝试访问对象的键值,但它不起作用。我以前做过这个,并且在堆栈溢出和其他地方阅读了很多东西,但它仍然不起作用。


这是我的代码:



  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'] = undefined


validate.response = undefined


validate.['done'] = undefined


validate.done = undefined


validateObj = {"done":true,"response":{"@type":"type.googleapis.com/google.apps.script.v1.ExecutionResponse","result":"Invalid Full Name.  Please enter a valid Full Name."}}


validateObj.response = undefined


validateObj.done = undefined

使用括号表示法时,我也尝试过使用单引号和双引号。


谁能帮我看看错误?


杨魅力
浏览 90回答 1
1回答

HUX布斯

您正在尝试从字符串访问responseand属性,因此您得到了. 您已尝试对响应执行操作,但随后返回到字符串,只需执行并访问该属性doneundefinedJSON.parseJSON.stringifyJSON.parsevar validateObj = JSON.parse(validate); // Now you have a deserialized objectLogger.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' ]);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript