代码之家  ›  专栏  ›  技术社区  ›  mY777

每个的Ajax JSON

  •  0
  • mY777  · 技术社区  · 7 年前

    大家好,我有一个简单的ajax请求,这也很好,但我必须循环trought数据并显示对象中的所有10个名称allready在堆栈中搜索它,但我的对象有点复杂。有什么建议可以解决吗?

    $( document ).ready(function() {
    $.ajax({
       url: 'XXXXXXX',
       data: {
          format: 'json'
       },
       error: function() {
       alert('ERROR');
       },
       sasdataType: 'json',
       success: function(data) {
        /*var $hotelname = $('<h1>').text(data.result.hotel[0].displayname);
            $('#hotel-name').append($hotelname)*/
        console.log(data);
       },
       type: 'GET'
    });
    });
    
    3 回复  |  直到 7 年前
        1
  •  3
  •   Rahul Sahu    7 年前

    希望这对你有帮助

    $.ajax({
       url: 'https://www.hrs.de/hotel/service/mmsuggest-group?query=k%C3%B6ln&language=de',
       data: {
          format: 'json'
       },
       error: function() {
       alert('ERROR');
       },
       sasdataType: 'json',
       success: function(data) {
        /*var $hotelname = $('<h1>').text(data.result.hotel[0].displayname);
            $('#hotel-name').append($hotelname)*/
        console.log(data);
      console.log(data.result);
    data.result.hotel.forEach(function(hd){console.log(hd.displayname)});
       },
       type: 'GET'
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        2
  •  1
  •   mY777    7 年前

    $(document).ready(function() {
      $.ajax({
        url: 'XXX',
        data: {
          format: 'json'
        },
        error: function() {
          alert('ERROR');
        },
        sasdataType: 'json',
        success: function(data) {
          $.each(data.result.hotel, function(index, value) {
            $('#hotel-name').append($('<h1>').text(value.displayname));
          });
        },
        type: 'GET'
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id='hotel-name'></div>

    您可以直接在酒店中循环,使用以下方法获取酒店名称 $.each

    $.each(data.result.hotel, function(index, value) {
      console.log(value.displayname);
    });
    
        3
  •  1
  •   SmartDev    7 年前

    如果我把结构计算好的话,应该是这样的:

    $.each(data.result.district, function(index, district) {
         $.each($(district), function(index, hotel) {
             console.log($(hotel).displayname);
         });
    });