我已将JSON数据存储在变量中。但在检索其显示意外的令牌时。
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);
}
}
有人可以帮助我,我错了,我得到了意想不到的令牌o错误。我搜索了解决方案,但无法解决。请帮帮我
答案 0 :(得分:3)
您的JSON数据无效,因为您在密钥const todosReducer = (state = [], action) => {
if (action.type === 'ADD_TODO') {
return {
id: action.payload.id,
text: action.payload.text,
completed: false
};
}
return state;
};
const visibilityFilterReducer = (state = 'SHOW_ALL', action) => {
// your visibility filter reducer logic
};
export default combineReducers({
todos: todosReducer,
visibilityFilter: visibilityFilterReducer
});
的值内部"main-header dark-bg"
而没有转义那些双引号。
成功
key1
或者在键值
中使用单引号作为属性值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>\"}]";
答案 1 :(得分:0)
按照以下方式制作你的json:
File.Exists