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文件中有比預期的另一種格式。如何正確地做到這一點?
這是發生在Node.js的代碼,或者你正在使用別的,盡我的回答如果你正在使用的NodeJS – Adrian
你可以看到,在HTML文件。但是不要緊 –