gpt4 book ai didi

javascript - JSON 中第 6 位的意外标记 i - React redux 应用程序

转载 作者:行者123 更新时间:2023-11-30 09:33:46 25 4
gpt4 key购买 nike

我正在创建一个简单的 React redux 应用程序我有 json 格式的数据,即 characters.json

问题是,当我启动应用程序时,我得到了JSON 中位置 6 的意外标记 i

你能告诉我这里出了什么问题吗?

[
{
id: 0,
name: "Superman",
strength: 10,
intelligence: 7,
speed: 9
},
{
id: 1,
name: "Batman",
strength: 7,
intelligence: 10,
speed: 6
},
{
id: 2,
name: "Wonderwoman",
strength: 5,
intelligence: 9,
speed: 7
},
{
id: 3,
name: "Flash",
strength: 5,
intelligence: 6,
speed: 10
},
{
id: 4,
name: "Green Lantern",
strength: 7,
intelligence: 8,
speed: 7
},
{
id: 5,
name: "Aquaman",
strength: 8,
intelligence: 7,
speed: 8
},
{
id: 6,
name: "Cyborg",
strength: 9,
intelligence: 8,
speed: 6
},
{
id: 7,
name: "Green Arrow",
strength: 5,
intelligence: 9,
speed: 7
},
{
id: 8,
name: "Hawkman",
strength: 5,
intelligence: 8,
speed: 8
},
{
id: 9,
name: "Supergirl",
strength: 10,
intelligence: 9,
speed: 7
}
]

这是主要 index.js 文件的代码

import React from 'react';
import ReactDOM from 'react-dom';

import App from './components/App';
import {createStore} from 'redux';
import {Provider} from 'react-redux';
import rootReducer from './reducers';

const store = createStore(rootReducer);
console.log('store.getState()',store.getState());

ReactDOM.render(
<Provider store={store}>
<App/>
</Provider>,
document.getElementById('root')
);

最佳答案

我猜这是因为键没有被引用,因为第一个键以 i 开头。

一些(大多数?)JSON 解析器不支持不带引号的键,因为规范(RFC、ECMA)要求键是带引号的字符串。

虽然在互操作性方面不太理想,但支持不带引号的键的解析器主要是为了方便。强烈建议不要依赖它并始终生成带有引号键的 JSON 文档。

[
{
"id": 0,
"name": "Superman",
"strength": 10,
"intelligence": 7,
"speed": 9
},
...
]

关于javascript - JSON 中第 6 位的意外标记 i - React redux 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44774164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com