如何在React js中通过本地JSON文件解析?

公园主义:

如何解析一个JSON文件以检索其所有数据并在我的代码中使用它?

我试过导入文件,只是尝试控制台记录它,但是它所做的只是打印对象{}:

import jsonData from "./file.json";
console.log(jsonData);

这是我的file.json的样子:

[
    {
      "id": 1,
      "gender": "Female",
      "first_name": "Helen",
      "last_name": "Nguyen",
      "email": "[email protected]",
      "ip_address": "227.211.25.18"
    }, {
      "id": 2,
      "gender": "Male",
      "first_name": "Carlos",
      "last_name": "Fowler",
      "email": "[email protected]",
      "ip_address": "214.248.201.11"
    }
]

我希望能够访问每个组件的名字和姓氏并将其打印在网站上。

fulvio:
var data = require('../../file.json'); // forward slashes will depend on the file location

var data = [
    {
      "id": 1,
      "gender": "Female",
      "first_name": "Helen",
      "last_name": "Nguyen",
      "email": "[email protected]",
      "ip_address": "227.211.25.18"
    }, {
      "id": 2,
      "gender": "Male",
      "first_name": "Carlos",
      "last_name": "Fowler",
      "email": "[email protected]",
      "ip_address": "214.248.201.11"
    }
];

for (var i = 0; i < data.length; i++)
{
	var obj = data[i];
	console.log("Name: " + obj.first_name + ", " + obj.last_name);
}

https://jsfiddle.net/c9wupvo6/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在JavaScript中读取外部本地JSON文件?

Android Java;如何将资产文件夹中的本地JSON文件解析为ListView

React:如何在React中重定向?

如何在React Native上从本地JSON文件获取数据?

如何将资产文件夹中的本地JSON文件解析为ListView?

如何在jsfiddle中添加本地json文件?

如何在节点js中解析JSON?

本地JSON文件未在React中解析

如何在Octave中解析json文件?

如何在React.js中解析FOUC

如何在React中导入json文件

如何在React Native中解析JSON数据

如何解析本地文件中的json数据?

在由create-react-app创建的环境中获取本地JSON文件时,JSON解析错误

如何通过单击按钮显示本地JSON文件中的数据?

如何从React.js中的本地Json文件获取图像网址

Django Bootstrap-如何从本地JSON文件解析变量并将其显示在.html模板中?

如何在Angular库中读取本地JSON文件?

如何在React JS中通过ID过滤组件

如何在React中解析外部json文件?

如何解析本地JSON文件

如何将json从本地json文件解析为android中的ListView?

如何使用本地 DTD 文件通过 lxml 解析 XML 文件?

如何在 react js 中映射 JSON 文件中数组中的数据?

如何在反应引导表中调用本地 json 文件

如何在 React 中映射 json 文件

如何使用swift从本地JSON文件中解析两个数组

如何从文件管理器中解析本地 json 文件?

如何从文件管理器中解析本地 json 文件?