简体   繁体   English

获取&#39;未捕获的SyntaxError:意外的令牌o <unknown file> :使用JSON.parse时为1&#39;

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

相关问题 使用JSON.parse时,&#39;未捕获的SyntaxError:意外的标记u&#39; - 'Uncaught SyntaxError: Unexpected token u' when using JSON.parse Uncaught SyntaxError:意外的令牌o-JSON.Parse - Uncaught SyntaxError: Unexpected token o - JSON.Parse Uncaught SyntaxError:意外的令牌o- JSON.Parse - Uncaught SyntaxError: Unexpected token o- JSON.Parse 未捕获到的SyntaxError:意外令牌[与JSON.parse - Uncaught SyntaxError: Unexpected token [ with JSON.parse "未捕获的 SyntaxError:带有 JSON.parse 的意外标记" - Uncaught SyntaxError: Unexpected token with JSON.parse JSON.parse 错误(未捕获的 SyntaxError:JSON 中的意外标记 o 在 position 1) - JSON.parse error (Uncaught SyntaxError: Unexpected token o in JSON at position 1) 使用JSON.parse时,出现“ SyntaxError:JSON中位置1的意外令牌” - I am getting “SyntaxError: Unexpected token ' in JSON at position 1” when using JSON.parse 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM