首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在React中导入JSON文件时出现数据格式错误

在React中导入JSON文件时出现数据格式错误
EN

Stack Overflow用户
提问于 2018-09-05 12:52:46
回答 2查看 1.5K关注 0票数 1

我试图在我的React项目中导入一个JSON文件,但得到了解析错误:

代码语言:javascript
复制
json file:testData.json
  {
    "data": {
      "articles": [
            {
            "id": "95c12a8f6c88953ca8f8a39da25546e6",
            "title": "Introducing React's Error Code System",
            "date": "Mon Jul 11 2016 00:00:00 GMT+0000 (UTC)",
            "authorId": "2c6aa2cfe3449467d329fa17d6ea230f",
            "body": "Building a better developer experience has been one of the things that React deeply cares about, and a crucial part of it is to detect anti-patterns/potential errors early and provide helpful error messages when things (may) go wrong. However, most of these only exist in development mode; in production, we avoid having extra expensive assertions and sending down full error messages in order to reduce the number of bytes sent over the wire."
             }
          ],
       "authors": [
            {
             "id": "d85577ea34ae50f2dac5347b5219aa23",
             "firstName": "Andrew",
             "lastName": "Clark",
             "website": "https://twitter.com/acdlite"
            }
         ]
      }
    }

DataApi.js文件:

代码语言:javascript
复制
export default class DataApi {
// property: rawData
  constructor(rawData) {
   this.rawData = rawData;
 }

   mapIntoObject(arr) {
      return arr.reduce((acc, curr) => {
      acc[curr.id] = curr;
      return acc;
      }, {});
    }
   getArticle() {
      return this.mapIntoObject(this.rawData.articles);
   }
   getAuthors() {
      return this.mapIntoObject(this.rawData.authors);
   }
 }

我尝试在这个文件中导入JSON数据:

代码语言:javascript
复制
import DataApi from "./DataApi"; // object to process data
import { data } from "./testData.json"; // raw data

// create a api object to host raw data
let api = new DataApi(data);

const articles = api.getArticle();

console.log(articles);

然后我得到了错误:(导入目录是正确的):

代码语言:javascript
复制
   2:13  error  Parsing error: Unexpected token, expected ";"

  1 | {
> 2 |   "articles": [
    |             ^
  3 |     {
  4 |       "id": "95c12a8f6c88953ca8f8a39da25546e6",
  5 |       "title": "Introducing React's Error Code System",

有什么问题吗?

EN

回答 2

Stack Overflow用户

发布于 2018-09-05 13:15:04

您可以导出默认设置

testData.json:

代码语言:javascript
复制
const data = {
"data": {
  "articles": [
        {
        "id": "95c12a8f6c88953ca8f8a39da25546e6",
        "title": "Introducing React's Error Code System",
        "date": "Mon Jul 11 2016 00:00:00 GMT+0000 (UTC)",
        "authorId": "2c6aa2cfe3449467d329fa17d6ea230f",
        "body": "Building a better developer experience has been one of the things that React deeply cares about, and a crucial part of it is to detect anti-patterns/potential errors early and provide helpful error messages when things (may) go wrong. However, most of these only exist in development mode; in production, we avoid having extra expensive assertions and sending down full error messages in order to reduce the number of bytes sent over the wire."
        }
      ],
  "authors": [
        {
        "id": "d85577ea34ae50f2dac5347b5219aa23",
        "firstName": "Andrew",
        "lastName": "Clark",
        "website": "https://twitter.com/acdlite"
        }
    ]
  }
}

export default data;

并且在导入时

安装了json-loader后,您可以使用

代码语言:javascript
复制
import data from "./testData.json";

或者,如果你已经使用create-react-app搭建了你的项目,这个模块已经包含在内了,你只需要导入你的json:

代码语言:javascript
复制
import data from "./testData";

安装json-loader的步骤

代码语言:javascript
复制
npm install --save-dev json-loader

并将以下配置添加到您的webpack.config.js中

webpack.config.js

代码语言:javascript
复制
module.exports = {
  module: {
    loaders: [
      {
        test: /\.json$/,
        loader: 'json-loader'
      }
    ]
  }
}
票数 1
EN

Stack Overflow用户

发布于 2018-09-05 13:01:26

你必须导出你的json数据,你的json数据应该是这样的。

代码语言:javascript
复制
export const data = {
"data": {
  "articles": [
        {
        "id": "95c12a8f6c88953ca8f8a39da25546e6",
        "title": "Introducing React's Error Code System",
        "date": "Mon Jul 11 2016 00:00:00 GMT+0000 (UTC)",
        "authorId": "2c6aa2cfe3449467d329fa17d6ea230f",
        "body": "Building a better developer experience has been one of the things that React deeply cares about, and a crucial part of it is to detect anti-patterns/potential errors early and provide helpful error messages when things (may) go wrong. However, most of these only exist in development mode; in production, we avoid having extra expensive assertions and sending down full error messages in order to reduce the number of bytes sent over the wire."
        }
      ],
  "authors": [
        {
        "id": "d85577ea34ae50f2dac5347b5219aa23",
        "firstName": "Andrew",
        "lastName": "Clark",
        "website": "https://twitter.com/acdlite"
        }
    ]
  }
}

导入时将.json更改为.js扩展名

代码语言:javascript
复制
import { data } from "./testData"; // raw data
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52177298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档