我有一个带有数据的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 :(得分: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" }
]