Javascript 如何从Node.js中的JSON文件创建对象
我已经加载了一个json文件,其中包含很多信息,而这些信息对于我将表呈现到只包含一些数据项的html页面的最终目的来说是不需要的 data.json看起来像Javascript 如何从Node.js中的JSON文件创建对象,javascript,json,node.js,loops,javascript-objects,Javascript,Json,Node.js,Loops,Javascript Objects,我已经加载了一个json文件,其中包含很多信息,而这些信息对于我将表呈现到只包含一些数据项的html页面的最终目的来说是不需要的 data.json看起来像 { "events": [ { "id": "181101322507029", "name": "Carnival Special", "type": "public", "coverPicture": "https://scontent.xx.fbcdn.net/v/t1.0.jpg?oh=b9eC", "prof
{ "events": [
{
"id": "181101322507029",
"name": "Carnival Special",
"type": "public",
"coverPicture": "https://scontent.xx.fbcdn.net/v/t1.0.jpg?oh=b9eC",
"profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-jpg?oh=32c4574",
"description": "Lots and lots of info blah blah",
"distance": "1546",
"startTime": "2017-11-18T22:00:00+0000",
"endTime": "2017-11-19T03:00:00+0000",
"timeFromNow": 476169,
"isCancelled": false,
"isDraft": false,
"category": null,
"ticketing": {
"ticket_uri": "https://nicevenue.co.uk/event/lime-of-your-life/"
},
"place": {
"id": "517137361697082",
"name": "Nice Venue",
"location": {
"city": "Nice City",
"country": "United Kingdom",
"latitude": 66.3445334,
"longitude": -4.7356667928,
"street": "The Blah Blah, Blah, Blah Road",
"zip": "AB1 2CD"
}
},
"stats": {
"attending": 68,
"declined": 0,
"maybe": 177,
"noreply": 336
},
"venue": {
"id": "517137361697082",
"name": "Nice Venue",
"about": "Situated in the blah blah aims to provide a friendly atmosphere, gourmet food and fine cocktails.",
"emails": [
"nicevenue@googlemail.com"
],
"coverPicture": "https://scontent.xx.fbcdn.net/v/t31.0-8/s78_o.jpg?oh=8f62cb6CB2",
"profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-1/c0.16n.jpg?oh=8ef5df00f31&oe=5AAF3935",
"category": "Bar",
"categoryList": [
"Bar",
"Restaurant"
],
"location": {
"city": "Nice City",
"country": "United Kingdom",
"latitude": 65.138604292331,
"longitude": -9.73566d867928,
"street": "The Blah Blah, Blah, Blah Road",
"zip": "AB1 2CD"
}
}
},
{
"id": "1896504087342309",
"name": "Mr Tea + Dj Dr SJ",
"type": "public",events
"
"
etc....
我需要遍历这些数据并创建一个新的对象(或数组),如
然后,我想使用一个express模板遍历这个对象,将信息呈现到html中的表标记中
我已经用hasProperty或使用low-dash_u2;.Map查看了in循环,但我真的不知道我是否正确地执行了此操作,甚至不知道我是否正在以最佳方式尝试执行此操作。谢谢
var resObj = JSON.parse("your json data");
resObj.forEach(function(event)){
var id = event.id;
var place = event.place;
//so on...and create new objects here.
}
然后,您可以在html中加载数据,或者从这个循环中生成一个html字符串,并将其附加到主div中
然后,您可以加载html中的数据,或从该循环中生成html字符串,并将其附加到主div中。如果您使用ES6或更高版本,则可以执行类似操作,使用新对象创建新数组: 如果您不使用ES6,lodash将具有_映射 我将使用以下将firstname转换为name的示例来保持简单:
var测试={
“学生”:[
{
“名字”:“伊恩”,
“年龄”:25岁
},
{
“名字”:“瑞克”,
“年龄”:25岁
}
]
}
var newList=test.students.map(数据=>({
name:data.firstname,
年龄:data.age
}));
console.log(newList)
如果您使用的是ES6或更高版本,您可以执行类似操作来使用新对象创建新数组:
如果您不使用ES6,lodash将具有_映射
我将使用以下将firstname转换为name的示例来保持简单:
var测试={
“学生”:[
{
“名字”:“伊恩”,
“年龄”:25岁
},
{
“名字”:“瑞克”,
“年龄”:25岁
}
]
}
var newList=test.students.map(数据=>({
name:data.firstname,
年龄:data.age
}));
console.log(newList)
您可以使用JSON.parse获取实际对象,然后使用for-in-loop从对象中提取数据。您可以使用JSON.parse获取实际对象,然后使用for-in-loop从对象中提取数据。此代码有错误。data.forEach(function(event)){SyntaxError:Unexpected token)var data=JSON.parse(data);data.forEach(function(event)){var id=events.id;var vention=events.place.name;var address=events.location.street+“”+events.location.city“”+events.location.zip;var-coverPicture=events.coverPicture;var-description=events.description;var-startTime=events.startTime;var-endTime=events.endTime;//依此类推……并在此处创建新对象。}我将var-data=JSON.parse(“data”);然后将其更改为data.events.forEach(函数(事件)){….但我仍然得到相同的错误-意外的标记..?您得到的json数据是什么?您可以将其粘贴到这里吗?{“事件”:[{“id”:“18110”,名称:““Lime of your Life”,“type”:“public”,“coverPicture”:“profilePicture”:“description”:“desc!”,“distance”:“1546”startTime:“2017-11-18T22:00:00+0000”,“endTime:“2017-11-19T03:00:00+0000”,“timeFromNow”:476169,“isCancelled”:false,“isDraft”:false,“category”:null,“ticketing”:{“ticket_uri”:“},这里的事件是一个数组仪式。因此var data=JSON.parse(“您的数据”)data.events.forEach(“您的代码”)我在代码中遇到错误。data.forEach(函数(事件)){SyntaxError:Unexpected token)var data=JSON.parse(数据);data.forEach(函数(事件)){var id=events.id;var venture=events.place.name;var address=events.location.street+”“+events.location.city”“+events.location.zip;var-coverPicture=events.coverPicture;var-description=events.description;var-startTime=events.startTime;var-endTime=events.endTime;//依此类推……并在此处创建新对象。}我将var-data=JSON.parse(“数据”);然后将其更改为data.events.forEach(函数(事件)){….但我仍然得到相同的错误-意外的标记..?您得到的json数据是什么?您可以将其粘贴到这里吗?{“事件”:[{“id”:“18110”,名称:““Lime of your Life”,“type”:“public”,“coverPicture”:“profilePicture”:“description”:“desc!”,“distance”:“1546”startTime:“2017-11-18T22:00:00+0000”,“endTime:“2017-11-19T03:00:00+0000”,“timeFromNow”:476169,“isCancelled”:false,“isDraft”:false,“category”:null,“ticketing”:{“ticket_uri”:“},这里的事件是一个数组仪式。因此var data=JSON.parse(“您的数据”)data.events.forEach(“您的代码”)
var resObj = JSON.parse("your json data");
resObj.forEach(function(event)){
var id = event.id;
var place = event.place;
//so on...and create new objects here.
}