在React中导入JSON文件,但数据格式错误

时间:2018-09-05 04:52:46

标签: json reactjs

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

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文件:

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数据:

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);

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

   2:13  error  Parsing error: Unexpected token, expected ";"

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

出什么问题了?

2 个答案:

答案 0 :(得分:0)

您必须导出json数据,您的json数据应如下所示。

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

import { data } from "./testData"; // raw data

答案 1 :(得分:0)

您可以导出默认值

testData.json:

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后,您可以使用

import data from "./testData.json";

或者如果您已经使用create-react-app来构建您的项目,则该模块已经包含在内,您只需导入json:

import data from "./testData";

要安装json-loader

npm install --save-dev json-loader

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

webpack.config.js

module.exports = {
  module: {
    loaders: [
      {
        test: /\.json$/,
        loader: 'json-loader'
      }
    ]
  }
}