简体   繁体   English

为什么我无法从 ReactJS 中的 JSON 文件中检索数据?

[英]Why can't I retrieve data from my JSON file in ReactJS?

I have a JSON file (named journey0.json) in which I store data as shown below.我有一个 JSON 文件(名为 Journey0.json),我在其中存储数据,如下所示。

{
"title": "journy title",
"date": "2020",
"homepageImage": "/images/journey1/TravelCardImage",
"introStory": "text",
"introMap": "",
"destinations": [
    {
        "destinationTitle": "Berlijn",
        "destinationStory": "destination story",
        "destinationPreviewImage": "/images/journey1/prague.jpg",
        "destinationImages": [
            {
                "original": "/images/journey1/HomeHeader.jpg",
                "thumbnail": "/images/journey1/HomeHeader.jpg"
            },
            {
                "original": "/images/journey1/HomeHeader.jpg",
                "thumbnail": "/images/journey1/HomeHeader.jpg"
            },
            {
                "original": "/images/journey1/HomeHeader.jpg",
                "thumbnail": "/images/journey1/HomeHeader.jpg"
            },
           ]
       }
   ]
 }

I then make an array of all similar JSON files in another file (which is.jsx) as seen below:然后,我在另一个文件(即.jsx)中创建一个包含所有类似 JSON 文件的数组,如下所示:

import * as journey0 from './journey0.json'

export const allJourneysArray = [
    journey0
]

export default allJourneysArray;

I try to access the data as seen below but for some reason the values remain undefined.我尝试访问如下所示的数据,但由于某种原因,这些值仍未定义。 What am I doing wrong?我究竟做错了什么?

import JourneyCard from '../journeyCard/JourneyCard';
import allJourneysArray from '../../../journeys/allJourneys';

class HomePageBody extends Component {
    render() {

        let journeyCards = [];

        for (var x in allJourneysArray) {
            journeyCards.push(
                <Link style={this.styles.link} to={'/' + x}>
                    <JourneyCard
                        title={allJourneysArray[x].title}
                        date={allJourneysArray[x].date}
                        homepageImage={allJourneysArray[x].homepageImage}/>
                </Link>
            )
        }

        return(
            <div style={this.styles.container}>
                <div style={this.styles.cardContainer}>
                    {journeyCards}
                </div>
            </div>
        );
    }

Try: import journey0 from './journey0.json'尝试: import journey0 from './journey0.json'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM