简体   繁体   English

JSON.parse 错误(未捕获的 SyntaxError:JSON 中的意外标记 o 在 position 1)

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

相关问题 未捕获到的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>) Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) 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 localStorage 和 JSON.parse 的问题,错误:Uncaught SyntaxError: Unexpected token [ in JSON at position 1430 - problem with localStorage and JSON.parse, error : Uncaught SyntaxError: Unexpected token [ in JSON at position 1430 JSON.parse() 导致错误:`SyntaxError: Unexpected token in JSON at position 0` - JSON.parse() causes error: `SyntaxError: Unexpected token in JSON at position 0`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM