I have stored JSON data in variable. but while retrieving its showing unexpected token o.
var somejson = "[{ \"key1\" : \"<header class=\"main-header dark-bg\"><div class=\"row\"></div></header>\"},{\"key2\" : \"<div class=\"row content clearfix worldwide dark-bg t--bg-5\"></div>\"}]";
var mainObject = JSON.parse(somejson);
console.log(mainObject);
for (var key in mainObject) {
//console.log("key"+key);
var innerObject = mainObject[key];
//console.log("inner object "+innerObject);
for (var innerKey in innerObject) {
var t = innerObject[innerKey];
console.log(t);
}
}
Can some one help me where i'm wrong, i'm gettting unexpected token o error. I searched for solution but couldn't able to resolve. please help me on this
Your JSON data is invalid since you are having "main-header dark-bg"
inside the value of key key1
without escaping those double quotes .
Make it
var somejson = "[{ \"key1\" : \"<header class=\\\"main-header dark-bg\\\"><div class=\\\"row\\\"></div></header>\"},{\"key2\" : \"<div class=\\\"row content clearfix worldwide dark-bg t--bg-5\\\"></div>\"}]";
Or use single quotes for attribute values inside the key value
var somejson = "[{ \"key1\" : \"<header class='main-header dark-bg'><div class='row'></div></header>\"},{\"key2\" : \"<div class='row content clearfix worldwide dark-bg t--bg-5'></div>\"}]";
使您的json如下所示:
"[{ "key1" : "<header class='main-header dark-bg'><div class='row'></div></header>"},{"key2" : "<div class='row content clearfix worldwide dark-bg t--bg-5'></div>"}]"
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.