我有一个包含数据的 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 文件的格式比预期的要好。如何正确执行?