Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 将Json数组映射到Json文件中_Reactjs - Fatal编程技术网

Reactjs 将Json数组映射到Json文件中

Reactjs 将Json数组映射到Json文件中,reactjs,Reactjs,我有下面的JSON文件。JSON中有一个数组(answers)。我想映射和渲染它们 { "qID": "177", "myQuestion": true, "qContent": "testing", "answers": [{ "anonymous": true, &q

我有下面的JSON文件。JSON中有一个数组(answers)。我想映射和渲染它们

    {
    "qID": "177",
    "myQuestion": true,
    "qContent": "testing",
    "answers": [{
            "anonymous": true,
            "aID": "184",
            "aContent": "test",
            "aDate": "Mon, 14 Sep 20 06:47:38 +0000",
            "myAnswer": false,
            "authorName": "abc",
            "aFeaturedImg": "..\/uploads\/407378vk053.jpg",
            "authorImg": "https:\/\/lh3.googleusercontent.com\/a-\/AOh14GjhhO-11Wktws9-FXBZoWk3vT8tRM2Sy7IQ4cnpw=s96-c"
        },
        {
            "anonymous": true,
            "aID": "184",
            "aContent": "test",
            "aDate": "Mon, 14 Sep 20 06:47:38 +0000",
            "myAnswer": false,
            "authorName": "abc",
            "aFeaturedImg": "..\/uploads\/407378vk053.jpg",
            "authorImg": "https:\/\/lh3.googleusercontent.com\/a-\/AOh14GjhhO-11Wktws9-FXBZoWk3Cv8tRM2Sy7IQ4cnpw=s96-c"
        }
    ]
}
我的代码是这样的。我想使用状态

函数问题({match}){


您需要将JSON解析为javascript对象,请尝试这样做

let parsed_object = JSON.parse(item);

然后您可以在
解析的_对象上使用
映射

您需要将JSON解析为javascript对象,请尝试这样做

let parsed_object = JSON.parse(item);
然后您可以在
解析的_对象上使用
映射

您需要检查“item”是否包含有效值

{item && item.answers.map(answer => (
             <Card key={answer.aID}>{answer.aContent}</Card>
        ))}
{item&&item.answers.map(answer=>(
{answer.aContent}
))}
您需要检查“项”是否包含有效值

{item && item.answers.map(answer => (
             <Card key={answer.aID}>{answer.aContent}</Card>
        ))}
{item&&item.answers.map(answer=>(
{answer.aContent}
))}

如果您不确定数据的
答案
键是否会被视为一个数组,您可以通过将其传递给

let数据={
“qID”:“177”,
“我的问题”:没错,
“qContent”:“测试”,
“答案”:[{
“匿名”:没错,
“援助”:“184”,
“aContent”:“test”,
“aDate”:“9月14日星期一06:47:38+0000”,
“我的答案”:错,
“authorName”:“abc”,
“aFeaturedImg”:“.\/uploads\/407378vk053.jpg”,
“authorImg”:“https:\/\/lh3.googleusercontent.com\/a-\/AOh14GjhhO-11Wktws9-FXBZoWk3vT8tRM2Sy7IQ4cnpw=s96-c”
},
{
“匿名”:没错,
“援助”:“184”,
“aContent”:“test”,
“aDate”:“9月14日星期一06:47:38+0000”,
“我的答案”:错,
“authorName”:“abc”,
“aFeaturedImg”:“.\/uploads\/407378vk053.jpg”,
“authorImg”:“https:\/\/lh3.googleusercontent.com\/a-\/AOh14GjhhO-11Wktws9-FXBZoWk3Cv8tRM2Sy7IQ4cnpw=s96-c”
}
]
}
类应用程序扩展了React.Component{
render(){
让answers=Array.from(data.answers)
返回(
{answers.map(ans=>{
返回{ans.aContent}

})} ) } } ReactDOM.render(,document.getElementById('root');

如果您不确定数据的
答案
键是否会被视为一个数组,您可以通过将其传递给

let数据={
“qID”:“177”,
“我的问题”:没错,
“qContent”:“测试”,
“答案”:[{
“匿名”:没错,
“援助”:“184”,
“aContent”:“test”,
“aDate”:“9月14日星期一06:47:38+0000”,
“我的答案”:错,
“authorName”:“abc”,
“aFeaturedImg”:“.\/uploads\/407378vk053.jpg”,
“authorImg”:“https:\/\/lh3.googleusercontent.com\/a-\/AOh14GjhhO-11Wktws9-FXBZoWk3vT8tRM2Sy7IQ4cnpw=s96-c”
},
{
“匿名”:没错,
“援助”:“184”,
“aContent”:“test”,
“aDate”:“9月14日星期一06:47:38+0000”,
“我的答案”:错,
“authorName”:“abc”,
“aFeaturedImg”:“.\/uploads\/407378vk053.jpg”,
“authorImg”:“https:\/\/lh3.googleusercontent.com\/a-\/AOh14GjhhO-11Wktws9-FXBZoWk3Cv8tRM2Sy7IQ4cnpw=s96-c”
}
]
}
类应用程序扩展了React.Component{
render(){
让answers=Array.from(data.answers)
返回(
{answers.map(ans=>{
返回{ans.aContent}

})} ) } } ReactDOM.render(,document.getElementById('root');


console.log(item.answers)
在渲染之前得到了什么?这是来自ajax的json数据吗?那么你的
item
状态是什么样子的?你从
console.log(item.answers)得到了什么
就在渲染之前?这是来自ajax的json数据吗?那么您的
状态如何?非常感谢您的快速回答。我想使用状态实现此功能。我修改了我的问题。您能帮我吗?您想将响应保存在组件状态并映射保留的
答案
数组吗在您的组件状态中。我尝试了以下操作。但仍然给出错误。[code]const fetchItem=async()=>{const data=await fetch(“”,requestOptions);const item=await data.json();setItem(item);const answers=Array.from(item.answers);setAnswers(answers);};返回({answers.map(answer=>({answer.qContent}))});您在问题中提供的数据这是API调用返回响应的方式非常感谢您的快速回答。我想使用状态实现此功能。我修改了我的问题。您能帮我吗?您想将响应保存在组件状态中,并映射组件状态中保留的
answers
数组吗尝试了以下操作。但仍然给出错误。[code]const fetchItem=async()=>{const data=await fetch(“”,requestOptions);const item=await data.json();setItem(item);const answers=Array.from(item.answers);setAnswers(answers);};返回({answers.map(answer=>({answer.qContent}))});您在问题中提供的数据是从API的callHi返回响应的方式。感谢您的回答。但这不起作用。请尝试在useEffect中挂接项useEffect(()=>{fetchItem();},[item]);您是否也尝试过在fetchItem中控制台.log(item)?以确保您拥有正确的数据HI。感谢您的回答。但这不起作用。尝试在useEffect中挂钩项useEffect(()=>{fetchItem();},[item]);您是否也尝试过在fetchItem中控制台.log(item)?以确保您拥有正确的数据