简体   繁体   English

如何在我的 React 应用程序中导入和使用 JSON 文件中的数据?

[英]How can I import and use data from a JSON file in my React app?

I've been programming in React for a couple days now and have come across an issue utilizing a JSON file.我已经在 React 中编程了几天,并且在使用 JSON 文件时遇到了一个问题。

I'm currently using a hard coded list of...我目前正在使用硬编码列表...

    const vendorList = [
        {
            id: 0,
            name: "Laerdal Medical Corporation",
            sections: [
                {
                    name: "Product",
                    payload: [
                        {id: 1, name: "ASL 5000 Lung Solution"},
                    ]
                },
                {
                    name: "Demos",
                    payload: [
                        {id: 1, name: "ASL 5000 Lung Solution", url: "ASL 5000 Lung Solution - High Fidelity Lungs for SimMan and SimBaby (laerdal.com)", type: "video"}
]

To populate some "VendorCards".填充一些“供应商卡”。 I now have a JSON file with the same data in it...我现在有一个 JSON 文件,其中包含相同的数据......

{
    "vendorList" : [
            {
                "id": 1,
                "name": "Laerdal Medical Corporation",
                "sections": [
                    {
                        "name": "Products",
                        "payload": [
                            {"id": 1, "name": "ASL 5000 Lung Solution"}
                        ]
                    },
                    {
                        "name": "Demos",
                        "payload": [
                            {"id": 1, "name": "ASL 5000 Lung Solution", "url": "ASL 5000 Lung Solution - High Fidelity Lungs for SimMan and SimBaby (laerdal.com)", "type": "video"}
                        ]
                }
            ],
            "tags": ["Tag 1","Tag 2"]
        }]

How do I go about changing from my hardcoded list, to my JSON object?如何从我的硬编码列表更改为 JSON object? I am able to import the JSON object after reading other stackoverflow questions using import myJson from './data.json' , but can't figure out how to put the json object in vendorlist . I am able to import the JSON object after reading other stackoverflow questions using import myJson from './data.json' , but can't figure out how to put the json object in vendorlist . Thanks for your time!谢谢你的时间!

From your comments it looks like the JSON is being imported using从您的评论看来,JSON 正在使用

import myJson from ./data.json';

which I then assume it is being automatically parsed.然后我假设它正在被自动解析。 (So no need for JSON.parse ) (所以不需要JSON.parse

After this, it is only a matter of pushing the data into vendorlist .在此之后,只需将数据推送到vendorlist

vendorlist.push(...myJson.vendorList);

If you don't want to mutate vendorlist you can also create a new array by combining the two.如果您不想改变vendorlist ,您也可以通过组合两者来创建一个新数组。

const newlist = [...vendorlist, ...myJson.vendorList];
// or
const newlist = vendorlist.concat(myJson.vendorList);

暂无
暂无

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

相关问题 我如何从一开始就在 React 中检索数据 (JSON),然后按照我的意愿将其导出到我的应用程序的任何其他文件中? - How can I retrieve data (JSON) in React from the beginning and then export it as I wish in any other file of my app? 如何使用Redux访问本地JSON文件以在整个React应用程序中使用? - How can I access a local JSON file with Redux to use throughout my React app? 如何使用AngularJS控制器从Google App Engine中获取数据,而不仅仅是从json文件中获取数据 - How can I use an AngularJS controller to get data from Google App Engine and not just from json file 如何从本地文件获取数据到我的 React 应用程序中? - How can I get data from a local file into my React app? 如何从 Firebase 获取数据到我的 React 应用程序中 - How can I fetch data from Firebase into my React App 如何导入 json 数据文件并在反应组件中使用它? - how to import json data file and use it in a react component? 如何将 JSON 数据中的图像导入 create-react-app - How to import images from JSON data into create-react-app 如何修复我的 React 应用程序中尝试导入的错误? - How can I fix attempted import error in my React App? 如何在我的 React Hooks 电子商务应用程序中使用 JSON 文件作为虚拟用户的数据库? - How do I use a JSON file as a database for dummy users in my React Hooks e-commerce app? 如何在反应中导入我的对象文件 - How can I import my objects file in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM