次のページを 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 文字列に 2 つの要素を追加する必要があります
- フライト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= "" ]
}