在React JS中从JSON对象获取数据时出错

时间:2019-09-23 11:55:48

标签: javascript json reactjs

我想从json对象中获取数据

{[{"Checkit":"list","st":[{"name":"Mike","ID":1,"Problem":"France - 6 Months"}]}]}

但是遇到下面提到的错误

const myObj ={[{"Checkit":"list","st":[{"name":"Mike","ID":1,"Problem":"France - 6 Months"}]}]};
const myObjStr = JSON.stringify(myObj);
console.log(myObjStr);

得到这个错误 SyntaxError: /src/index.js: Unexpected token (1:171)

2 个答案:

答案 0 :(得分:2)

问题在于传递给String化的JSON。您始终可以使用一些在线编辑器来验证JSON,以检查其格式。 您可以检查

{"Checkit":"list","st":[{"name":"Mike","ID":1,"Problem":"France - 6 Months"}]}

[{"Checkit":"list","st":[{"name":"Mike","ID":1,"Problem":"France - 6 Months"}]}]

答案 1 :(得分:2)

您创建的对象中没有导致错误的键

const myObj1 =[{"Checkit":"list","st":[{"name":"Mike","ID":1,"Problem":"France - 6 Months"}]}];

const myObj2 ={key: [{"Checkit":"list","st":[{"name":"Mike","ID":1,"Problem":"France - 6 Months"}]}]};


const myObjStr1 = JSON.stringify(myObj1);
const myObjStr2 = JSON.stringify(myObj2);

console.log(myObjStr1);
console.log(myObjStr2);