Ajax显示JSON提要

时间:2017-11-10 13:06:57

标签: jquery json ajax

我想使用这个json端点创建一个汽车模型。

如何创建汽车对象,然后为每个汽车对象提供一组一致的属性?

我在这里有什么: 错误的输出,你会看到很多“未定义”,因为它循环整个JSON字符串,并且有6个项目不是我们要求的3个变量中的项目。

抱歉,我对此并不熟悉。

Demo

    $.get( "series-data.json", function( obj ) {
      for(x in obj.data.series){
        var series_icon = obj.data.series[x].series_icon;
        var display_order = obj.data.series[x].display_order;
        var brand = obj.data.series[x].brand;

        var html = "<ul>";
          html += "<li>"+ series_icon +"</li>";
          html += "<li>"+ display_order +"</li>";
          html += "<li>"+ brand +"</li>";
          html += "</ul>";

        $('#output').append(html);
      }
    });

6 个答案:

答案 0 :(得分:1)

您可以使用解决方案

$.get("http://pdi.bmw.staging.oliver.solutions/data/series-data.json", function(obj) {
  for (x in obj.data.series) {   
    if(typeof obj.data.series[x].series_icon != "undefined"){
     $('#output').append(`<ul>
        <li>${obj.data.series[x].series_icon}</li>
        <li>${obj.data.series[x].display_order}</li>
        <li>${obj.data.series[x].brand}</li>
      </ul>`);
    }  
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>

我使用了ES6 template literals

如果您不想使用ES6 template literals,请尝试以下代码

$.get("http://pdi.bmw.staging.oliver.solutions/data/series-data.json", function(obj) {
  for (x in obj.data.series) {   
    if(typeof obj.data.series[x].series_icon != "undefined"){
     $('#output').append("<ul>" + 
        "<li>" + obj.data.series[x].series_icon + "</li>" +
        "<li>" + obj.data.series[x].display_order + "</li>" +
        "<li>" + obj.data.series[x].brand + "</li>" +
      "</ul>");
    }  
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>

希望这会对你有所帮助。

答案 1 :(得分:1)

从您的JSON我们可以看到series_icondisplay_orderbrand仅存在于密钥1 series2 series下,因此您需要更改代码相应

您需要检查每个迭代keys下这些object的存在,这就是您的代码应该如何:

  var cars = [];
  for(x in obj.data.series){
    if(obj.data.series[x].series_icon && obj.data.series[x].display_order && obj.data.series[x].brand){
        var series_icon = obj.data.series[x].series_icon;
        var display_order = obj.data.series[x].display_order;
        var brand = obj.data.series[x].brand;

        //This code will create a Car object and push it in the cars array
        let car = {};
        for(key in obj.data.series[x]){
           car[key] = obj.data.series[x][key];
        }
        cars.push(car);

        var html = "<ul>";
        html += "<li>"+ series_icon +"</li>";
        html += "<li>"+ display_order +"</li>";
        html += "<li>"+ brand +"</li>";
        html += "</ul>";

        $('#output').append(html);
    }
  }

<强>演示:

这是一个演示:

&#13;
&#13;
var obj = {
  "data": {
    "series": {
      "brochure_image": "range.jpg",
      "brochure_id": "DLR_CR",
      "brochure_name": "Range",
      "background_image": "range.jpg",
      "link": "http://www.bmw.co.uk/vc/ncc/xhtml/start/startWithModelSelection.faces?productType=1&brand=BM&market=GB&country=GB&locale=en_GB",
      "link_text": "Explore the showrooms",
      "BMW i": {
        "series_icon": "i.png",
        "display_order": 12,
        "medium_images": "i-i3.png",
        "brand": "BMW i",
        "i3": {
          "series": "BMW i3",
          "medium_images": "i-i3.png",
          "brochure_image": "i-i3.png",
          "brochure_id": "DLR_I3",
          "brochure_name": "i3",
          "background_image_mobile": "i3.jpg",
          "background_image": "i3.jpg",
          "link": "http://www.bmw.co.uk/en_GB/new-vehicles/bmw-i/i3/2013/introduction.html",
          "m_series": false,
          "hybrid": false,
          "mapped_body_type": "Hatchback",
          "body_type": "Hatchback I01",
          "body_style_id": "I01"
        },
        "i8": {
          "series": "BMW i8",
          "medium_images": "i-i8.png",
          "brochure_image": "i-i8.png",
          "brochure_id": "DLR_I8",
          "brochure_name": "i8",
          "background_image_mobile": "i8.jpg",
          "background_image": "i8.jpg",
          "link": "http://www.bmw.co.uk/en_GB/new-vehicles/bmw-i/i8/2013/introduction.html",
          "m_series": false,
          "hybrid": false,
          "mapped_body_type": "Coupe",
          "body_type": "Coupé I12",
          "body_style_id": "I12"
        }
      },
      "1 Series": {
        "series_icon": "1-series.png",
        "display_order": 1,
        "medium_images": "1-series-3-door-sports-hatch.png",
        "brand": "BMW",
        "3-door Sports hatch": {
          "series": "1 Series",
          "medium_images": "1-series-3-door-sports-hatch.png",
          "brochure_image": "1-series-sports-hatch.png",
          "brochure_id": "DLR_F21",
          "brochure_name": "1 Series 3 -door Sports Hatch",
          "background_image_mobile": "1Series_3door.jpg",
          "background_image": "1Series_3door.jpg",
          "link": "http://www.bmw.co.uk/en_GB/new-vehicles/1/3-door/2015/introduction.html",
          "m_series": false,
          "hybrid": false,
          "mapped_body_type": "Hatchback",
          "body_type": "3-door Sports hatch F21",
          "body_style_id": "F21"
        },
        "5-door Sports hatch": {
          "series": "1 Series",
          "medium_images": "1-series-5-door-sports-hatch.png",
          "brochure_image": "1-series-sports-hatch.png",
          "brochure_id": "DLR_F20F",
          "brochure_name": "1 Series 5 -door Sports Hatch",
          "background_image_mobile": "1Series_5door.jpg",
          "background_image": "1Series_5door.jpg",
          "link": "http://www.bmw.co.uk/en_GB/new-vehicles/1/5-door/2015/introduction.html",
          "m_series": false,
          "hybrid": false,
          "mapped_body_type": "Hatchback",
          "body_type": "5-door Sports hatch F20",
          "body_style_id": "F20"
        }
      },
      "2 Series": {
        "series_icon": "2-series.png",
        "display_order": 2,
        "medium_images": "2-series-coup.png",
        "brand": "BMW",
        "Active Tourer": {
          "series": "2 Series",
          "medium_images": "2-series-active-tourer.png",
          "brochure_image": "2-series-active-tourer.png",
          "brochure_id": "DLR_F45",
          "brochure_name": "2 Series Active Tourer",
          "background_image_mobile": "2Series_ActiveTourer.jpg",
          "background_image": "2Series_ActiveTourer.jpg",
          "link": "http://www.bmw.co.uk/en_GB/new-vehicles/2/activetourer/2014-update/introduction.html",
          "m_series": false,
          "hybrid": false,
          "mapped_body_type": "Hatchback",
          "body_type": "GT F45",
          "body_style_id": "F45"
        }
      }
    }
  }
}

var cars = [];
for (x in obj.data.series) {
  if (obj.data.series[x].series_icon && obj.data.series[x].display_order && obj.data.series[x].brand) {
    var series_icon = obj.data.series[x].series_icon;
    var display_order = obj.data.series[x].display_order;
    var brand = obj.data.series[x].brand;

    //This code will create a Car object and push it in the cars array
    let car = {};
    for (key in obj.data.series[x]) {
      car[key] = obj.data.series[x][key];
    }
    cars.push(car);

    var html = "<ul>";
    html += "<li>" + series_icon + "</li>";
    html += "<li>" + display_order + "</li>";
    html += "<li>" + brand + "</li>";
    html += "</ul>";

    $('#output').append(html);
  }
}
console.log(cars.length);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="output"></div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

首先,您必须严格检查是否存在价值,这就是您获得undefined的原因。我做了一些更正,并希望这是你正在寻找的。

Working fiddle example

console.clear()

$.get("http://pdi.bmw.staging.oliver.solutions/data/series-data.json", function(obj) {
 var data = JSON.parse(obj.body); 
   //console.log(typeof data.data.series);
var series = data.data.series;
  for (x in series) {
    // console.log(obj.data.series[x]);
    // console.log(obj.data.series[x].series_icon);
     var html = "";
        if(typeof series[x]  === "object") {
        var series_icon = series[x].series_icon;
        var display_order = series[x].display_order;
        var brand = series[x].brand;
      console.log(series_icon, display_order, brand);

      html += "<ul>";
    html += "<li>" + series_icon + "</li>";
    html += "<li>" + display_order + "</li>";
    html += "<li>" + brand + "</li>";
    html += "</ul>";
    }

console.log(html);


    $('#output').append(html);


  }

});
<div id="output"></div>

答案 3 :(得分:0)

我不确定我是否理解正确。但是,我认为,您必须删除不包含任何series_icondisplay_orderbrand属性的对象。这意味着您只会显示必要的对象。

以下是代码:

$.get( "series-data.json", function( obj ) {
  for(x in obj.data.series){
    if (obj.data.series[x].series_icon
        && obj.data.series[x].display_order
        && obj.data.series[x].brand) {

      var series_icon = obj.data.series[x].series_icon;
      var display_order = obj.data.series[x].display_order;
      var brand = obj.data.series[x].brand;

      var html = "<ul>";
        html += "<li>"+ series_icon +"</li>";
        html += "<li>"+ display_order +"</li>";
        html += "<li>"+ brand +"</li>";
        html += "</ul>";

      $('#output').append(html);
    }
  }
});

答案 4 :(得分:0)

&#13;
&#13;
var resp = {
  "data": {
"series": {
  "brochure_image": "range.jpg",
  "brochure_id": "DLR_CR",
  "brochure_name": "Range",
  "background_image": "range.jpg",
  "link": "http://www.bmw.co.uk/vc/ncc/xhtml/start/startWithModelSelection.faces?productType=1&brand=BM&market=GB&country=GB&locale=en_GB",
  "link_text": "Explore the showrooms",
  "BMW i": {
    "series_icon": "i.png",
    "display_order": 12,
    "medium_images": "i-i3.png",
    "brand": "BMW i",
    "i3": {
      "series": "BMW i3",
      "medium_images": "i-i3.png",
      "brochure_image": "i-i3.png",
      "brochure_id": "DLR_I3",
      "brochure_name": "i3",
      "background_image_mobile": "i3.jpg",
      "background_image": "i3.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/bmw-i/i3/2013/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Hatchback",
      "body_type": "Hatchback I01",
      "body_style_id": "I01"
    },
    "i8": {
      "series": "BMW i8",
      "medium_images": "i-i8.png",
      "brochure_image": "i-i8.png",
      "brochure_id": "DLR_I8",
      "brochure_name": "i8",
      "background_image_mobile": "i8.jpg",
      "background_image": "i8.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/bmw-i/i8/2013/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Coupe",
      "body_type": "Coupé I12",
      "body_style_id": "I12"
    }
  },
  "1 Series": {
    "series_icon": "1-series.png",
    "display_order": 1,
    "medium_images": "1-series-3-door-sports-hatch.png",
    "brand": "BMW",
    "3-door Sports hatch": {
      "series": "1 Series",
      "medium_images": "1-series-3-door-sports-hatch.png",
      "brochure_image": "1-series-sports-hatch.png",
      "brochure_id": "DLR_F21",
      "brochure_name": "1 Series 3 -door Sports Hatch",
      "background_image_mobile": "1Series_3door.jpg",
      "background_image": "1Series_3door.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/1/3-door/2015/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Hatchback",
      "body_type": "3-door Sports hatch F21",
      "body_style_id": "F21"
    },
    "5-door Sports hatch": {
      "series": "1 Series",
      "medium_images": "1-series-5-door-sports-hatch.png",
      "brochure_image": "1-series-sports-hatch.png",
      "brochure_id": "DLR_F20F",
      "brochure_name": "1 Series 5 -door Sports Hatch",
      "background_image_mobile": "1Series_5door.jpg",
      "background_image": "1Series_5door.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/1/5-door/2015/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Hatchback",
      "body_type": "5-door Sports hatch F20",
      "body_style_id": "F20"
    }
  },
  "2 Series": {
    "series_icon": "2-series.png",
    "display_order": 2,
    "medium_images": "2-series-coup.png",
    "brand": "BMW",
    "Active Tourer": {
      "series": "2 Series",
      "medium_images": "2-series-active-tourer.png",
      "brochure_image": "2-series-active-tourer.png",
      "brochure_id": "DLR_F45",
      "brochure_name": "2 Series Active Tourer",
      "background_image_mobile": "2Series_ActiveTourer.jpg",
      "background_image": "2Series_ActiveTourer.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/2/activetourer/2014-update/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Hatchback",
      "body_type": "GT F45",
      "body_style_id": "F45"
    },
    "Coupé": {
      "series": "2 Series",
      "medium_images": "2-series-coup.png",
      "brochure_image": "2-series-coup-convertible.png",
      "brochure_id": "DLR_F22",
      "brochure_name": "2 Series Coupé",
      "background_image_mobile": "2Series_Coupe.jpg",
      "background_image": "2Series_Coupe.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/2/Coupe/2013/Introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Coupe",
      "body_type": "Coupe F22",
      "body_style_id": "F22"
    },
    "Convertible": {
      "series": "2 Series",
      "medium_images": "2-series-convertible.png",
      "brochure_image": "2-series-coup-convertible.png",
      "brochure_id": "DLR_F23",
      "brochure_name": "2 Series Convertible",
      "background_image_mobile": "2Series_Convertible.jpg",
      "background_image": "2Series_Convertible.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/2/convertible/2014/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Convertible",
      "body_type": "Convertible F23",
      "body_style_id": "F23"
    },
    "Gran Tourer": {
      "series": "2 Series",
      "medium_images": "2-series-gran-tourer.png",
      "brochure_image": "2-series-gran-tourer.png",
      "brochure_id": "DLR_F46",
      "brochure_name": "2 Series Gran Tourer",
      "background_image_mobile": "2Series_GranTourer.jpg",
      "background_image": "2Series_GranTourer.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/2/grantourer/2015/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Hatchback",
      "body_type": "MPV F46",
      "body_style_id": "F46"
    }
  },
  "3 Series": {
    "series_icon": "3-series.png",
    "display_order": 3,
    "medium_images": "3-series-saloon.png",
    "brand": "BMW",
    "Saloon": {
      "series": "3 Series",
      "medium_images": "3-series-saloon.png",
      "brochure_image": "3-series-saloon-touring.png",
      "brochure_id": "DLR_F30",
      "brochure_name": "3 Series Saloon",
      "background_image_mobile": "3Series_Saloon.jpg",
      "background_image": "3Series_Saloon.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/3/saloon/2015/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Saloon",
      "body_type": "Saloon F30",
      "body_style_id": "F30"
    },
    "Touring": {
      "series": "3 Series",
      "medium_images": "3-series-touring.png",
      "brochure_image": "3-series-saloon-touring.png",
      "brochure_id": "DLR_F31",
      "brochure_name": "3 Series Touring",
      "background_image_mobile": "3Series_Touring.jpg",
      "background_image": "3Series_Touring.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/3/touring/2015/Introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Touring",
      "body_type": "Touring F31",
      "body_style_id": "F31"
    },
    "Gran Turismo": {
      "series": "3 Series",
      "medium_images": "3-series-gran-turismo.png",
      "brochure_image": "3-series-gran-turismo.png",
      "brochure_id": "DLR_3GT",
      "brochure_name": "3 Series Gran Turismo",
      "background_image_mobile": "3Series_GranTurismo.jpg",
      "background_image": "3Series_GranTurismo.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/3/gran-turismo/2013/Introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Touring",
      "body_type": "GT F34",
      "body_style_id": "F34"
    }
  },
  "4 Series": {
    "series_icon": "4-series.png",
    "display_order": 4,
    "medium_images": "4-series-coup.png",
    "brand": "BMW",
    "Gran Coupé": {
      "series": "4 Series",
      "medium_images": "4-series-gran-coup.png",
      "brochure_image": "4-series-gran-coup.png",
      "brochure_id": "DLR_F36",
      "brochure_name": "4 Series Gran Coupé",
      "background_image_mobile": "4Series_GranCoupe.jpg",
      "background_image": "4Series_GranCoupe.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/4/grancoupe/2014/Introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Coupe",
      "body_type": "Coupe F36",
      "body_style_id": "F36"
    },
    "Coupé": {
      "series": "4 Series",
      "medium_images": "4-series-coup.png",
      "brochure_image": "4-series-coup-convertible.png",
      "brochure_id": "DLR_F32",
      "brochure_name": "4 Series Coupé",
      "background_image_mobile": "4Series_Coupe.jpg",
      "background_image": "4Series_Coupe.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/4/coupe/2013/Introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Coupe",
      "body_type": "Coupe F32",
      "body_style_id": "F32"
    },
    "Convertible": {
      "series": "4 Series",
      "medium_images": "4-series-convertible.png",
      "brochure_image": "4-series-coup-convertible.png",
      "brochure_id": "DLR_F33",
      "brochure_name": "4 Series Convertible",
      "background_image_mobile": "4Series_Convertible.jpg",
      "background_image": "4Series_Convertible.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/4/convertible/2013/Introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Convertible",
      "body_type": "Convertible F33",
      "body_style_id": "F33"
    }
  },
  "5 Series": {
    "series_icon": "5-series.png",
    "medium_images": "5-series-saloon.png",
    "display_order": 5,
    "brand": "BMW",
    "Saloon": {
      "series": "5 Series",
      "medium_images": "5-series-saloon.png",
      "brochure_image": "5-series-saloon.png",
      "brochure_id": "DLR_F10",
      "brochure_name": "5 Series Saloon",
      "background_image_mobile": "5Series_Saloon.jpg",
      "background_image": "5Series_Saloon.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/5/saloon/2013/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Saloon",
      "body_type": "Saloon F10",
      "body_style_id": "F10"
    },
    "Touring": {
      "series": "5 Series",
      "medium_images": "5-series-touring.png",
      "brochure_image": "5-series-touring.png",
      "brochure_id": "DLR_F11",
      "brochure_name": "5 Series Touring",
      "background_image_mobile": "5Series_Touring.jpg",
      "background_image": "5Series_Touring.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/5/touring/2013/Introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Touring",
      "body_type": "Touring F11",
      "body_style_id": "F11"
    },
    "Gran Turismo": {
      "series": "5 Series",
      "medium_images": "5-series-gran-turismo.png",
      "brochure_image": "5-series-gran-turismo.png",
      "brochure_id": "DLR_5GT",
      "brochure_name": "5 Series Gran Turismo",
      "background_image_mobile": "5Series_GranTurismo.jpg",
      "background_image": "5Series_GranTurismo.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/5/gran-turismo/2013/Introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Touring",
      "body_type": "GT F07",
      "body_style_id": "F07"
    }
  },
  "6 Series": {
    "series_icon": "6-series.png",
    "medium_images": "6-series-coup.png",
    "display_order": 6,
    "brand": "BMW",
    "Coupé": {
      "series": "6 Series",
      "medium_images": "6-series-coup.png",
      "brochure_image": "6-series-convertible-coupe-grancoupe.png",
      "brochure_id": "DLR_F13",
      "brochure_name": "6 Series Coupé",
      "background_image_mobile": "6Series_Coupe.jpg",
      "background_image": "6Series_Coupe.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/6/coupe/2014/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Coupe",
      "body_type": "Coupe F13",
      "body_style_id": "F13"
    },
    "Convertible": {
      "series": "6 Series",
      "medium_images": "6-series-convertible.png",
      "brochure_image": "6-series-convertible-coupe-grancoupe.png",
      "brochure_id": "DLR_F12",
      "brochure_name": "6 Series Convertible",
      "background_image_mobile": "6Series_Convertible.jpg",
      "background_image": "6Series_Convertible.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/6/convertible/2014/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Convertible",
      "body_type": "Convertible F12",
      "body_style_id": "F12"
    },
    "Gran Coupé": {
      "series": "6 Series",
      "medium_images": "6-series-gran-coup.png",
      "brochure_image": "6-series-convertible-coupe-grancoupe.png",
      "brochure_id": "DLR_F06",
      "brochure_name": "6 Series Gran Coupé",
      "background_image_mobile": "6Series_GranCoupe.jpg",
      "background_image": "6Series_GranCoupe.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/6/grancoupe/2014/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Coupe",
      "body_type": "Coupe F06",
      "body_style_id": "F06"
    }
  },
  "7 Series": {
    "series_icon": "7-series.png",
    "display_order": 7,
    "medium_images": "7-series-saloon.png",
    "brand": "BMW",
    "Saloon": {
      "series": "7 Series",
      "medium_images": "7-series-saloon.png",
      "brochure_image": "7-series-saloon.png",
      "brochure_id": "DLR_G11",
      "brochure_name": "7 Series Saloon",
      "background_image_mobile": "7Series_Saloon.jpg",
      "background_image": "7Series_Saloon.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/7/saloon/2015/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Saloon",
      "body_type": "Saloon G11",
      "body_style_id": "G11"
    }
  },
  "X": {
    "series_icon": "x.png",
    "display_order": 8,
    "medium_images": "x-x6.png",
    "brand": "BMW",
    "X1": {
      "series": "X1",
      "medium_images": "x-x1.png",
      "brochure_image": "x-x1.png",
      "brochure_id": "DLR_F48",
      "brochure_name": "X1",
      "background_image_mobile": "x1.jpg",
      "background_image": "x1.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/x/x1/2015/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "SUV",
      "body_type": "S.A.V. F48",
      "body_style_id": "F48"
    },
    "X3": {
      "series": "X3",
      "medium_images": "x-x3.png",
      "brochure_image": "x-x3.png",
      "brochure_id": "DLR_X3",
      "brochure_name": "X3",
      "background_image_mobile": "x3.jpg",
      "background_image": "x3.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/x/x3/2014/Introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "SUV",
      "body_type": "S.A.V. F25",
      "body_style_id": "F25"
    },
    "X4": {
      "series": "X4",
      "medium_images": "x-x4.png",
      "brochure_image": "x-x4.png",
      "brochure_id": "DLR_X4",
      "brochure_name": "X4",
      "background_image_mobile": "x4.jpg",
      "background_image": "x4.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/x/x4/2014/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "SUV",
      "body_type": "Sport Activity Coupe F26",
      "body_style_id": "F26"
    },
    "X5": {
      "series": "X5",
      "medium_images": "x-x5.png",
      "brochure_image": "x-x5.png",
      "brochure_id": "DLR_F15",
      "brochure_name": "X5",
      "background_image_mobile": "x5.jpg",
      "background_image": "x5.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/x/x5/2013/start.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "SUV",
      "body_type": "S.A.V. F15",
      "body_style_id": "F15"
    },
    "X6": {
      "series": "X6",
      "medium_images": "x-x6.png",
      "brochure_image": "x-x6.png",
      "brochure_id": "DLR_X6",
      "brochure_name": "X6",
      "background_image_mobile": "x6.jpg",
      "background_image": "x6.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/x/x6/2014/introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "SUV",
      "body_type": "Sport Activity Coupe F16",
      "body_style_id": "F16"
    }
  },
  "Z4": {
    "series_icon": "z4.png",
    "medium_images": "z4-z4.png",
    "display_order": 9,
    "brand": "BMW",
    "Z4": {
      "series": "Z4",
      "medium_images": "z4-z4.png",
      "brochure_image": "z4-z4.png",
      "brochure_id": "DLR_E89",
      "brochure_name": "Z4",
      "background_image_mobile": "z4.jpg",
      "background_image": "z4.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/z4/roadster/2012/Introduction.html",
      "m_series": false,
      "hybrid": false,
      "mapped_body_type": "Convertible",
      "body_type": "Roadster E89",
      "body_style_id": "E89"
    }
  },
  "M": {
    "series_icon": "m.png",
    "medium_images": "m-m3-saloon.png",
    "medium_images_xdrive": "m-x6-m.png",
    "display_order": 10,
    "brand": "BMW",
    "M2 Coupé": {
      "series": "2 Series",
      "medium_images": "m-m2-coupe.png",
      "brochure_image": "m-m2-coupe.jpg",
      "brochure_id": "",
      "brochure_name": "M2 Coupé",
      "background_image_mobile": "M2_Coupe.jpeg",
      "background_image": "M3.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/M/m2-coupe/2015/introduction.html",
      "m_series": true,
      "hybrid": false,
      "mapped_body_type": "Coupe",
      "body_type": "Coupe F87",
      "body_style_id": "F87"
    },
    "M3 Saloon": {
      "series": "3 Series",
      "medium_images": "m-m3-saloon.png",
      "brochure_image": "m-m3-saloon-m4-coupe-convertible.png",
      "brochure_id": "DLR_F80",
      "brochure_name": "M3 Saloon",
      "background_image_mobile": "M3.jpg",
      "background_image": "M3.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/z4/roadster/2012/Introduction.html",
      "m_series": true,
      "hybrid": false,
      "mapped_body_type": "Saloon",
      "body_type": "Saloon F80",
      "body_style_id": "F80"
    },
    "M4 Coupé": {
      "series": "4 Series",
      "medium_images": "m-m4-coup.png",
      "brochure_image": "m-m3-saloon-m4-coupe-convertible.png",
      "brochure_id": "DLR_F82",
      "brochure_name": "M4 Coupé",
      "background_image_mobile": "M4_coupe.jpg",
      "background_image": "M4_coupe.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/M/m4-coupe/2014/Introduction.html",
      "m_series": true,
      "hybrid": false,
      "mapped_body_type": "Coupe",
      "body_type": "Coupe F82",
      "body_style_id": "F82"
    },
    "M4 Convertible": {
      "series": "4 Series",
      "medium_images": "m-m4-convertible.png",
      "brochure_image": "m-m3-saloon-m4-coupe-convertible.png",
      "brochure_id": "DLR_F83",
      "brochure_name": "M4 Convertible",
      "background_image_mobile": "M4_Convertible.jpg",
      "background_image": "M4_Convertible.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/M/m4-convertible/2014/introduction.html",
      "m_series": true,
      "hybrid": false,
      "mapped_body_type": "Convertible",
      "body_type": "Convertible F83",
      "body_style_id": "F83"
    },
    "M5 Saloon": {
      "series": "5 Series",
      "medium_images": "m-m5-saloon.png",
      "brochure_image": "m-m5-saloon.png",
      "brochure_id": "DLR_F10M",
      "brochure_name": "M5 Saloon",
      "background_image_mobile": "M5.jpg",
      "background_image": "M5.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/M/m5-saloon/2013/Introduction.html",
      "m_series": true,
      "hybrid": false,
      "mapped_body_type": "Saloon",
      "body_type": "Saloon F10",
      "body_style_id": "F10"
    },
    "M6 Coupé": {
      "series": "6 Series",
      "medium_images": "m-m6-coup.png",
      "brochure_image": "m-m6-convertible-coup-grancoup.png",
      "brochure_id": "DLR_F13M",
      "brochure_name": "M6 Coupé",
      "background_image_mobile": "M6_Coupe.jpg",
      "background_image": "M6_Coupe.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/M/m6-coupe/2015/introduction.html",
      "m_series": true,
      "hybrid": false,
      "mapped_body_type": "Coupe",
      "body_type": "Coupe F13",
      "body_style_id": "F13"
    },
    "M6 Convertible": {
      "series": "6 Series",
      "medium_images": "m-m6-convertible.png",
      "brochure_image": "m-m6-convertible-coup-grancoup.png",
      "brochure_id": "DLR_F12M",
      "brochure_name": "M6 Convertible",
      "background_image_mobile": "M6_Convertible.jpg",
      "background_image": "M6_Convertible.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/M/m6-convertible/2015/introduction.html",
      "m_series": true,
      "hybrid": false,
      "mapped_body_type": "Convertible",
      "body_type": "Convertible F12",
      "body_style_id": "F12"
    },
    "M6 Gran Coupé": {
      "series": "6 Series",
      "medium_images": "m-m6-gran-coup.png",
      "brochure_image": "m-m6-convertible-coup-grancoup.png",
      "brochure_id": "DLR_F06M",
      "brochure_name": "M6 Convertible",
      "background_image_mobile": "M6_GranCoupe.jpg",
      "background_image": "M6_GranCoupe.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/M/m6-gran-coupe/2015/introduction.html",
      "m_series": true,
      "hybrid": false,
      "mapped_body_type": "Coupe",
      "body_type": "Coupe F06",
      "body_style_id": "F06"
    },
    "X5 M": {
      "series": "X5",
      "medium_images": "m-x5-m.png",
      "brochure_image": "m-x5-x6-m.png",
      "brochure_id": "DLR_X5M",
      "brochure_name": "X5 M",
      "background_image_mobile": "x5m.jpg",
      "background_image": "x5m.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/M/x5-m/2014/introduction.html",
      "m_series": true,
      "hybrid": false,
      "mapped_body_type": "SUV",
      "body_type": "S.A.V. F85",
      "body_style_id": "F85"
    },
    "X6 M": {
      "series": "X6",
      "medium_images": "m-x6-m.png",
      "brochure_image": "m-x5-x6-m.png",
      "brochure_id": "DLR_X6M",
      "brochure_name": "X6 M",
      "background_image_mobile": "x6m.jpg",
      "background_image": "x6m.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/M/x6-m/2014/introduction.html",
      "m_series": true,
      "hybrid": false,
      "mapped_body_type": "SUV",
      "body_type": "Sport Activity Coupe E71",
      "body_style_id": "E71"
    }
  },
  "Hybrid": {
    "series_icon": "hybrid.png",
    "medium_images": "hybrid-activehybrid-5.png",
    "display_order": 11,
    "brand": "BMW",
    "ActiveHybrid 5": {
      "series": "5 Series",
      "medium_images": "hybrid-activehybrid-5.png",
      "brochure_image": "5-series-saloon.png",
      "brochure_id": "DLR_F10",
      "brochure_name": "ActiveHybrid 5",
      "background_image_mobile": "Hybrid.jpg",
      "background_image": "Hybrid.jpg",
      "link": "http://www.bmw.co.uk/en_GB/new-vehicles/5/activehybrid-5/2013/Introduction.html",
      "m_series": false,
      "hybrid": true,
      "mapped_body_type": "Saloon",
      "body_type": "Saloon F10",
      "body_style_id": "F10"
    }
  }
}
  }
}

for (serie in resp.data.series) {
  if (resp.data.series[serie].series_icon) {
    $('#series').append(`
      <ul>
        <li>${resp.data.series[serie].series_icon}</li>
        <li>${resp.data.series[serie].display_order}</li>
        <li>${resp.data.series[serie].brand}</li>
      </ul>
    `)
  }
}
&#13;
<div id="series"></div>
<script src="https://unpkg.com/jquery"></script>
&#13;
&#13;
&#13;

答案 5 :(得分:-1)

试试这个

c := colly.NewCollector()
c.Post("https://example.com/login", map[string]string{"user": "x", "pass": "y"})