$.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. Trying to access part (itemType is specific earlier in the script) under the products_and_categories part of JSON file.
Tables under products and categories (after i click on 'object' in console):
Accessories: (8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Hats: (6) [{…}, {…}, {…}, {…}, {…}, {…}]
Shirts: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
Pants: (5) [{…}, {…}, {…}, {…}, {…}]
As Sajeeb commented, " stockData
is not a valid JSON string." In fact, stockData
is likely a JS object because
1) it's a field on parsed JSON, and JSON doesn't usually contain nested JSON
2) JSON.parse
casts it's param to a string. A JS object stringified is '[object Object]'
. Parsing this would produce the error you saw, 'Unexpected token o in JSON at position 1'.
If my assumption is right, then all you need to do is remove the nested JSON.parse
:
$.getJSON(staticMS, function(data) {
stockData = data.products_and_categories;
console.log(stockData);
var typeData = stockData;
console.log(typeData.itemType);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.