Express.js:不能在json文件中设置正确的数据格式

Express.js : Can't set a correct format of data in json file

本文关键字:设置 数据格式 文件 js 不能 json Express      更新时间:2023-09-26

我有一个包含数据的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文件似乎有另一种格式比预期的。如何正确地做呢?

我找到了正确的格式:

[
  { "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" }
]