[英]Getting 'Uncaught SyntaxError: Unexpected token o in <unknown file>:1' when using JSON.parse
I'm setting a variable to be equal to JSON text like so: 我将变量设置为等于JSON文本,如下所示:
var httpResponseBackup = {"findItemsByKeywordsResponse":[{"searchResult":["nada"]}]}
However, when I run my cloud code, it gives me this error: 但是,当我运行我的云代码时,它给了我这个错误:
Uncaught SyntaxError: Unexpected token o in <unknown file>:1
Based on what I've found by googling this error, it has to do with the following line of code: 根据我通过Google搜索此错误找到的内容,它与以下代码行有关:
var ebayResponse = JSON.parse(httpResponseBackup);
Am I formatting the JSON text in httpResponseBackup
incorrectly? 我是否错误地格式化了httpResponseBackup
的JSON文本?
JSON.parse
expects a string, stringified JSON. JSON.parse
需要一个字符串,字符串化JSON。 You are passing in something that's already a JSON object. 您传递的内容已经是JSON对象。 Therefore you can set your response to the object: 因此,您可以设置对该对象的响应:
var ebayResponse = httpResponseBackup;
Alternatively you could set your httpReponseBackup to the string value: 或者,您可以将httpReponseBackup设置为字符串值:
var httpResponseBackup = '{"findItemsByKeywordsResponse":[{"searchResult":["nada"]}]}'
docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse docs: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Given: 鉴于:
var httpResponseBackup = {"findItemsByKeywordsResponse":[{"searchResult":["nada"]}]};
var httpResponseBackupString = '{"findItemsByKeywordsResponse":[{"searchResult":["nada"]}]}';
Then: 然后:
JSON.stringify(httpResponseBackup) == httpResponseBackupString
And: 和:
JSON.parse(httpResponseBackupString)
will return a new object with the same structure as httpResponseBackup
. JSON.parse(httpResponseBackupString)
将返回一个与httpResponseBackup
结构相同的新对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.