通过Ajax/jQuery检索更复杂的JSON

通过Ajax/jQuery检索更复杂的JSON,jquery,ajax,json,jsonp,Jquery,Ajax,Json,Jsonp,我试图通过AJAX从JSON接收数据,但JSON结构更复杂,而不仅仅是一个值/键 以下是我被卡住的代码: $.ajax('data.json', { crossDomain: true, dataType: "json", success: function(data, text, xhqr) { $.each(data, function(i, item) { console.log(data.Cars); $("body").htm

我试图通过AJAX从JSON接收数据,但JSON结构更复杂,而不仅仅是一个值/键

以下是我被卡住的代码:

$.ajax('data.json', {
    crossDomain: true,
    dataType: "json",
    success: function(data, text, xhqr) {
      $.each(data, function(i, item) {
      console.log(data.Cars);
      $("body").html(data.Cars)
    });
 }
});
JSON:

理想情况下,我希望数据以以下表格格式显示:

有人能帮助我如何接收或读取json值/键吗? 非常感谢

$.ajax({
$.ajax({
    url: 'data.json',
    crossDomain: true,
    dataType: "json"
}).done(function(data) {
    var table = $('<table />'),
        thead = $('<thead />'),
        tbody = $('<tbody />');

    thead.append(
        $('<tr />').append(
            $('<th />', {text: 'Car'}),
            $('<th />', {text: 'Type'}),
            $('<th />', {text: 'Airbag'}),
            $('<th />', {text: 'Radio'}),
            $('<th />', {text: 'Convertible'}),
            $('<th />', {text: 'Total'})
        )
    );

    $.each(data.Cars, function(car, specs) {
        tbody.append(
            $('<tr />').append(
                $('<td />', {text: car}),
                $('<td />', {text: specs.Type}),
                $('<td />', {text: specs.features.Airbag}),
                $('<td />', {text: specs.features.Radio}),
                $('<td />', {text: specs.features.Convertible}),
                $('<td />', {text: specs.Total})
            )
        );
    });

    $('body').append( table.append(thead, tbody) );
});
url:'data.json', 跨域:是的, 数据类型:“json” }).完成(功能(数据){ 变量表=$(''), thead=$(''), t正文=$(''); thead.append( $('')。追加( $('',{text:'Car'}), $('',{text:'Type'}), $('',{text:'安全气囊'}), $('',{text:'Radio'}), $('',{text:'可转换'}), $('',{text:'Total'}) ) ); 美元每辆(数据车、功能车、规格){ tbody.append( $('')。追加( $('',{text:car}), $('',{text:specs.Type}), $('',{text:specs.features.Airbag}), $('',{text:specs.features.Radio}), $('',{text:specs.features.Convertible}), $('',{text:specs.Total}) ) ); }); $('body').append(table.append(thead,tbody)); });

这是一个奇怪的$.ajax调用?太神奇了。非常感谢你!!想象一下,会有另一种类型/关键的东西,比如边车旁的卡车。电话是这样的吗?“$。每个(数据、汽车、卡车、功能(汽车、规格){…});'嗯,诸如此类,很难说清楚,但是如果你使用上面的代码并尝试遵循变量,插入一些控制台日志等。理解迭代和访问属性等是如何工作的并不难。一旦您了解了它的工作原理,您就可以通过查看JSON并在此基础上创建迭代和元素来轻松地完成这类工作。
$.ajax({
    url: 'data.json',
    crossDomain: true,
    dataType: "json"
}).done(function(data) {
    var table = $('<table />'),
        thead = $('<thead />'),
        tbody = $('<tbody />');

    thead.append(
        $('<tr />').append(
            $('<th />', {text: 'Car'}),
            $('<th />', {text: 'Type'}),
            $('<th />', {text: 'Airbag'}),
            $('<th />', {text: 'Radio'}),
            $('<th />', {text: 'Convertible'}),
            $('<th />', {text: 'Total'})
        )
    );

    $.each(data.Cars, function(car, specs) {
        tbody.append(
            $('<tr />').append(
                $('<td />', {text: car}),
                $('<td />', {text: specs.Type}),
                $('<td />', {text: specs.features.Airbag}),
                $('<td />', {text: specs.features.Radio}),
                $('<td />', {text: specs.features.Convertible}),
                $('<td />', {text: specs.Total})
            )
        );
    });

    $('body').append( table.append(thead, tbody) );
});