2013-01-15 38 views
0

我有一个数据,这是我应该加载到阵列中的以下格式的JSON文件:Express.js:无法设置数据的JSON文件正确的格式

var jobs = [ 
    { ID: 'grt34hggdggf', Employer: 'A1', Title: 'tobi1', Location: 'Los Angeles, CA', Department: 'department', OpeningDate: '2012-12-26T01:29Z', ClosingDate: '2013-04-26T01:29Z', MinSalary: '30000', MaxSalary: '60000', Description: 'bla-gghrf-bla' }, 
    { ID: 'grt34hgwerwgf', Employer: 'A2', Title: 'tobi2', Location: 'Los Angeles, CA', Department: 'department', OpeningDate: '2012-12-26T01:29Z', ClosingDate: '2013-04-26T01:29Z', MinSalary: '30000', MaxSalary: '60000', Description: 'bla-gghrf-bla' } 
]; 

所以,这种格式适合我并与意见合作。 哪种格式应该有我的json文件。我尝试以下方法:

{ 
    "jobs":[ 
    { "ID": "grt34hggdggf", "Employer": "A1", "Title": "tobi1", "Location": "Los Angeles, CA", "Department": "department", "OpeningDate": "2012-12-26T01:29Z", "ClosingDate": "2013-04-26T01:29Z", "MinSalary": "30000", "MaxSalary": "60000", "Description": "bla-gghrf-bla" }, 
    { "ID": "grt34hggdggf", "Employer": "A2", "Title": "tobi1", "Location": "Los Angeles, CA", "Department": "department", "OpeningDate": "2012-12-26T01:29Z", "ClosingDate": "2013-04-26T01:29Z", "MinSalary": "30000", "MaxSalary": "60000", "Description": "bla-gghrf-bla" } 

    ] 
} 

但是当我尝试在视图做“的forEach”我得到一个错误:

TypeError: F:\tmp\express-master\examples\ejs\views\result.html:5 
    3| 
    4| <table> 
>> 5| <% jobs.forEach(function(job){ %> 
    6|  <tr> 
    7|   <td> 
    8|    <a href="/job/<%= job.ID %>"><%= job.Title %></a> 

Object #<Object> has no method 'forEach' 

如此,看来,JSON文件中有比预期的另一种格式。如何正确地做到这一点?

+0

这是发生在Node.js的代码,或者你正在使用别的,尽我的回答如果你正在使用的NodeJS – Adrian

+0

你可以看到,在HTML文件。但是不要紧 –

回答

0

我找到正确的格式,在我的情况:

[ 
    { "ID": "grt34hggdggf", "Employer": "A1", "Title": "tobi1", "Location": "Los Angeles, CA", "Department": "department", "OpeningDate": "2012-12-26T01:29Z", "ClosingDate": "2013-04-26T01:29Z", "MinSalary": "30000", "MaxSalary": "60000", "Description": "bla-gghrf-bla" }, 
    { "ID": "fgert", "Employer": "A2", "Title": "tobi2", "Location": "Los Angeles, CA", "Department": "department", "OpeningDate": "2012-12-26T01:29Z", "ClosingDate": "2013-04-26T01:29Z", "MinSalary": "30000", "MaxSalary": "60000", "Description": "bla11-ggh777rf-bla" } 
] 
相关问题