[英]JSON.parse error (Uncaught SyntaxError: Unexpected token o in JSON at position 1)
$.getJSON(staticMS, function(data) {
stockData = data.products_and_categories;
console.log(stockData);
var typeData = JSON.parse(stockData)
console.log(typeData.itemType);
});
Not sure whats wrong with this, I keep getting a "Uncaught SyntaxError: Unexpected token o in JSON at position 1" error.不知道这有什么问题,我不断收到“Uncaught SyntaxError: Unexpected token o in JSON at position 1”错误。 Trying to access part (itemType is specific earlier in the script) under the products_and_categories part of JSON file.
尝试访问 JSON 文件的 products_and_categories 部分下的部分(itemType 在脚本前面是特定的)。
Tables under products and categories (after i click on 'object' in console):产品和类别下的表格(在我单击控制台中的“对象”后):
Accessories: (8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]配件:(8) [{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}]
Hats: (6) [{…}, {…}, {…}, {…}, {…}, {…}]帽子:(6) [{...}, {...}, {...}, {...}, {...}, {...}]
Shirts: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]衬衫:(7) [{...}, {...}, {...}, {...}, {...}, {...}, {...}]
Pants: (5) [{…}, {…}, {…}, {…}, {…}]裤子:(5) [{…}, {…}, {…}, {…}, {…}]
As Sajeeb commented, " stockData
is not a valid JSON string."正如 Sajeeb 评论的那样,“
stockData
不是有效的 JSON 字符串。” In fact, stockData
is likely a JS object because事实上,
stockData
很可能是一个 JS object 因为
1) it's a field on parsed JSON, and JSON doesn't usually contain nested JSON 1)它是解析 JSON 上的一个字段,并且 JSON 通常不包含嵌套的 JSON
2) JSON.parse
casts it's param to a string. 2)
JSON.parse
将其参数转换为字符串。 A JS object stringified is '[object Object]'
. JS object 字符串化为
'[object Object]'
。 Parsing this would produce the error you saw, 'Unexpected token o in JSON at position 1'.解析这将产生您看到的错误,“在 position 1 处的 JSON 中出现意外的令牌 o”。
If my assumption is right, then all you need to do is remove the nested JSON.parse
:如果我的假设是正确的,那么您需要做的就是删除嵌套的
JSON.parse
:
$.getJSON(staticMS, function(data) {
stockData = data.products_and_categories;
console.log(stockData);
var typeData = stockData;
console.log(typeData.itemType);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.