2013-04-04 61 views
1

我有一個JSON文件沒有數組名稱的JSON數組?

{ 
    "info":[ 
     { 
     "Name":"Noob Here1", 
     "Department":"Language", 
     "Sex":"Male", 
     "Basic_Salary":"175", 
     "ESI":"58", 
     "Employee_PF":"60.50", 
     "Bonus":"2.60" 
     }, 
     { 
     "Name":"Noob Here2", 
     "Department":"Employee_PF", 
     "Sex":"Female", 
     "Basic_Salary":"10.5", 
     "ESI":"4.0", 
     "Employee_PF":"20", 
     "Bonus":"0.5" 
     }, 
     { 
     "Name":"Noob Here3", 
     "Department":"Physics", 
     "Sex":"Male", 
     "Basic_Salary":"165", 
     "ESI":"55", 
     "Employee_PF":"875", 
     "Bonus":"200" 
     } 
    ] 
} 

我使用的getJSON

loadData(myFile).done(function (data1) { 

    // check if we acutally get something back and if the data has the info property 
    if (data1 && data1.info) { 
     data = data1; 
     var html = ""; 
     var head = ""; 
     $.each(data1.info[0], function (key, value) { 
      html += "<td><input data-bind='value:" + key + "'></td>"; 
      head += "<th>" + key + "</th>"; 
     }); 
     html += "<td><input data-bind='value: result'/></td>"; 
     head += "<th>Result</th>"; 
     $("#div1 thead").append(head); 
     $("#div1").append("<tr></tr>"); 
     $("#div1 tr").append(html); 
    } 
}); 

注意採取從它的數據:MYFILE屬性給出JSON文件的名稱

和將數據追加到DOM

我可以使用像

一個JSON
{ 
    [ 
     { 
     "Name":"Noob Here1", 
     "Department":"Language", 
     "Sex":"Male", 
     "Basic_Salary":"175", 
     "ESI":"58", 
     "Employee_PF":"60.50", 
     "Bonus":"2.60" 
     }, 
     { 
     "Name":"Noob Here2", 
     "Department":"Employee_PF", 
     "Sex":"Female", 
     "Basic_Salary":"10.5", 
     "ESI":"4.0", 
     "Employee_PF":"20", 
     "Bonus":"0.5" 
     }, 
     { 
     "Name":"Noob Here3", 
     "Department":"Physics", 
     "Sex":"Male", 
     "Basic_Salary":"165", 
     "ESI":"55", 
     "Employee_PF":"875", 
     "Bonus":"200" 
     } 
    ] 
} 

如果是如何稱呼呢?

+0

數據1 [0] [0]給出菜鳥here1 – Tyranicangel 2013-04-04 12:38:00

+0

刪除。信息和數據來做,這是 – 1Mayur 2013-04-04 12:38:13

+0

到這裏看看: http://stackoverflow.com/questions/6964387/how-to-parse-json-array-with-no-object-name – 2013-04-04 12:38:13

回答

3

使用JSONLintJSLint,看看你的語法是正確的,你會得到這樣的:

Parse error on line 1: 
{ [  {   
-----^ 
Expecting 'STRING', '}' 

雖然有可能無論如何解析它,你永遠也不會做到這一點。

正如評論指出由Marco S.,您可以改用this

[ 
    { 
     "Name":"Noob Here1", 
     "Department":"Language", 
     "Sex":"Male", 
     "Basic_Salary":"175", 
     "ESI":"58", 
     "Employee_PF":"60.50", 
     "Bonus":"2.60" 
    }, 
    ... 
] 

,並獲得它像

result[0]["Name"]