我正在尝试将以下页面提取到 JSON 对象:
http://www.sarajevo-airport.ba/mobile/letovi.php?vrsta=odlasci&dest=Beograd&lang=bos
var mybody = document.getElementsByTagName('body')[0];
var table = mybody.getElementsByTagName('table')[0];
var jsonObj = [];
for (var i = 0, row; row = table.rows[i]; i++)
{
var cell2 = row.cells.length == '1' ? '' : row.cells[1].textContent.trim();
jsonObj.push( { what: row.cells[0].textContent.trim(), value:cell2 });
}
console.log(jsonObj);
让我找到这个 JSON 对象
[
Object { what= "Beograd" , value= "" },
Object { what= "Broj leta" , value= "JU 109, JA 1376" },
Object { what= "Avio-kompanija" , value= "JAT AIRWAYS" },
Object { what= "Tip aviona" , value= "AT72" },
Object { what= "Planirano vrijeme" , value= "06:20" },
Object { what= "Status leta" , value= "Odletio" },
Object { what= "" , value= "" },
Object { what= "Beograd" , value= "" },
Object { what= "Broj leta" , value= "SOP 4121" },
Object { what= "Avio-kompanija" , value= "SOLINAIR LTD" },
Object { what= "Tip aviona" , value= "SF34" },
Object { what= "Planirano vrijeme" , value= "16:35" },
Object { what= "Status leta" , value= "Odletio" },
Object { what= "" , value= "" }
]
由于此页面包含有关 2 个航班的信息,我需要在 JSON 字符串中添加两个元素来描述
- 航班1
- 飞行2
那么我怎样才能得到这样的东西:
{ "Flight1": [
Object { what= "Beograd" , value= "" },
Object { what= "Broj leta" , value= "JU 109, JA 1376" },
Object { what= "Avio-kompanija" , value= "JAT AIRWAYS" },
Object { what= "Tip aviona" , value= "AT72" },
Object { what= "Planirano vrijeme" , value= "06:20" },
Object { what= "Status leta" , value= "Odletio" },
Object { what= "" , value= "" }],
"Flight2": [
Object { what= "Beograd" , value= "" },
Object { what= "Broj leta" , value= "SOP 4121" },
Object { what= "Avio-kompanija" , value= "SOLINAIR LTD" },
Object { what= "Tip aviona" , value= "SF34" },
Object { what= "Planirano vrijeme" , value= "16:35" },
Object { what= "Status leta" , value= "Odletio" },
Object { what= "" , value= "" ]
}