json字段 react,如何解析React js中的本地JSON文件?

How can I parse through a JSON file retrieving all its data and using it in my code?

I've tried importing the file and just tried console logging it, but all it does is print Object {}:

import jsonData from "./file.json";

console.log(jsonData);

This is what my file.json looks like:

[

{

"id": 1,

"gender": "Female",

"first_name": "Helen",

"last_name": "Nguyen",

"email": "hnguyen0@bloomberg.com",

"ip_address": "227.211.25.18"

}, {

"id": 2,

"gender": "Male",

"first_name": "Carlos",

"last_name": "Fowler",

"email": "cfowler1@gnu.org",

"ip_address": "214.248.201.11"

}

]

I'd want to be able to access the first and last name of each component and print those on the website.

解决方案var data = require('../../file.json'); // forward slashes will depend on the file location

for(var i = 0; i < data.length; i++) {

var obj = data[i];

console.log("Name: " + obj.first_name + ", " + obj.last_name);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值