浏览我的Json对象

时间:2017-01-22 14:21:58

标签: javascript json

我的脚本中有以下Json对象:

var antennas = 'antennas [ :{"id":"66175", "nom_com":"PARIS-8E-ARRONDISSEMENT", "nom_dept":"PARIS", "nom_reg":"ILE-DE-FRANCE", "generation":"2G", "emr_lb_systeme":"GSM 900", "coordonnes":["48.8663888889","2.32305555556"], "coord":"48° 51′ 59"N 2° 19′ 23"E"},{"id":"66176", "nom_com":"PARIS-8E-ARRONDISSEMENT", "nom_dept":"PARIS", "nom_reg":"ILE-DE-FRANCE", "generation":"4G", "emr_lb_systeme":"LTE 800", "coordonnes":["48.8663888889","2.32305555556"], "coord":"48° 51′ 59"N 2° 19′ 23"E"}]';

我想迭代每个元素。

我做了这段代码:

var antennas = 'antennas [ :{"id":"66175", "nom_com":"PARIS-8E-ARRONDISSEMENT", "nom_dept":"PARIS", "nom_reg":"ILE-DE-FRANCE", "generation":"2G", "emr_lb_systeme":"GSM 900", "coordonnes":["48.8663888889","2.32305555556"], "coord":"48° 51′ 59"N 2° 19′ 23"E"},{"id":"66176", "nom_com":"PARIS-8E-ARRONDISSEMENT", "nom_dept":"PARIS", "nom_reg":"ILE-DE-FRANCE", "generation":"4G", "emr_lb_systeme":"LTE 800", "coordonnes":["48.8663888889","2.32305555556"], "coord":"48° 51′ 59"N 2° 19′ 23"E"}]';
var antennasMap = JSON.parse(antennas);

for (var key in antennasMap) {
    console.log(key);
}

但控制台给我这个错误:

  

SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符

1 个答案:

答案 0 :(得分:1)

您的JSON字符串有多个问题,因此无法解析为javascript对象:

  • 括号格式错误
  • 字符串包含coords字符串中未转义的引号

我使用http://jsonlint.com/来查找和解决问题

这里有一个有效的JSON字符串,接近你所拥有的:

var antennas = '{"antennas":[{"id":"66175","nom_com":"PARIS-8E-ARRONDISSEMENT","nom_dept":"PARIS","nom_reg":"ILE-DE-FRANCE","generation":"2G","emr_lb_systeme":"GSM 900","coordonnes":["48.8663888889","2.32305555556"],"coord":"48° 51′ 59\\"N 2° 19′ 23\\"E"},{"id":"66176","nom_com":"PARIS-8E-ARRONDISSEMENT","nom_dept":"PARIS","nom_reg":"ILE-DE-FRANCE","generation":"4G","emr_lb_systeme":"LTE 800","coordonnes":["48.8663888889","2.32305555556"],"coord":"48° 51′ 59\\" N 2° 19′ 23\\"E"}]}'

哪个解析为这个javascript对象:

{
    "antennas": [{
        "id": "66175",
        "nom_com": "PARIS-8E-ARRONDISSEMENT",
        "nom_dept": "PARIS",
        "nom_reg": "ILE-DE-FRANCE",
        "generation": "2G",
        "emr_lb_systeme": "GSM 900",
        "coordonnes": ["48.8663888889", "2.32305555556"],
        "coord": "48° 51′ 59\"N 2° 19′ 23\"E"
    }, {
        "id": "66176",
        "nom_com": "PARIS-8E-ARRONDISSEMENT",
        "nom_dept": "PARIS",
        "nom_reg": "ILE-DE-FRANCE",
        "generation": "4G",
        "emr_lb_systeme": "LTE 800",
        "coordonnes": ["48.8663888889", "2.32305555556"],
        "coord": "48° 51′ 59\"N 2° 19′ 23\"E"
    }]
}

以下是将记录输出到控制台的示例代码。



var antennas = '{"antennas":[{"id":"66175","nom_com":"PARIS-8E-ARRONDISSEMENT","nom_dept":"PARIS","nom_reg":"ILE-DE-FRANCE","generation":"2G","emr_lb_systeme":"GSM 900","coordonnes":["48.8663888889","2.32305555556"],"coord":"48° 51′ 59\\"N 2° 19′ 23\\"E"},{"id":"66176","nom_com":"PARIS-8E-ARRONDISSEMENT","nom_dept":"PARIS","nom_reg":"ILE-DE-FRANCE","generation":"4G","emr_lb_systeme":"LTE 800","coordonnes":["48.8663888889","2.32305555556"],"coord":"48° 51′ 59\\"N 2° 19′ 23\\"E"}]}';

var antennasMap = JSON.parse(antennas);

antennasMap.antennas.forEach (function (record) {
  console.log(record);
})