0

我正在尝试将以下页面提取到 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. 航班1
  2. 飞行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= "" ]

}

4

3 回答 3

0

Object不是有效的 JSON 元素。

您可以在此处验证您的 JSON:http: //jsonlint.com/

于 2013-03-05T21:30:42.350 回答
0

您可以检查<hr>元素以遍历您的对象:

var mybody = document.getElementsByTagName('body')[0];
var table = mybody.getElementsByTagName('table')[0];

var jsonObj = [], flights = {}, numFlights = 1;

for (var i = 0, row; row = table.rows[i]; i++) 
{
    if (row.cells[0].innerHTML == "<hr>") {
        flights["Flight"+(numFlights++)] = jsonObj;
        jsonObj = [];
    }
    else {
       var cell2 = row.cells.length == '1' ? '' : row.cells[1].textContent.trim();
       jsonObj.push( { what: row.cells[0].textContent.trim(), value:cell2 });
    }
}
console.log(flights);
于 2013-03-05T21:59:54.610 回答
0

您正在做的是创建基本的 javascript 对象。顺便说一句,你可以这样做:

var jsonObject = {}; // create object
jsonObject["foo"] = ["a", "b"]; // create associative indexes, so called members
jsonObject.bar = {}
jsonObject.something = 10;

要将其序列化为 JSON,最好的选择是JSON.stringify()将 js 对象转换为 JSON 字符串,以及jQuery.parseJSON()将 JSON 字符串转换回 js 对象。您将需要jQuery来使用这些方法。

于 2013-03-05T21:34:03.297 回答