Converted xml to json using a script :

{ "offers":[ { "offer":[ { "attr":{ "airline":{ "value":"Аэрофлот" }, "airline_code":{ "value":"SU" }, "title":{ "value":"Во Францию от 18662 рублей! Специальное предложение от авиакомпании Аэрофлот" }, "id":{ "value":22098 }, "href":{ "value":"https://www.aviasales.ru/offers/vo-frantsiu-ot-18662-rublei-spetsialnoe-predlozhenie-ot-aviakompanii-aeroflot" }, "sale_date_begin":{ "value":1483488000 }, "sale_date_end":{ "value":1484092800 }, "flight_date_begin":{ "value":1483488000 }, "flight_date_end":{ "value":1490400000 }, "link":{ "value":"https://hydra.aviasales.ru/adaptors/special_offer?iata=SU&utm_source=www&locale=ru&url=http%3A%2F%2Fwww.aeroflot.ru%2Fru-ru%2Fspecial_offers%2F" } }, "description":[ { "text":[ null ] } ], "conditions":[ { "text":"<ul><li>Цены на авиабилеты указаны с учетом топливных, аэропортовых и государственных сборов.</li><li>Количество авиабилетов на каждом рейсе по данному тарифу ограничено.</li><li>Полные условия продажи авиабилетов на сайте авиакомпании.</li></ul>" } ], "route":[ { "attr":{ "from_iata":{ "value":"MOW" }, "to_iata":{ "value":"PAR" }, "from_name":{ "value":"Москва" }, "to_name":{ "value":"Париж" }, "class":{ "value":"эконом" }, "oneway_price":{ "value":null }, "roundtrip_price":{ "value":"от 18662 рублей" } }, "text":[ null ] } ] } ] } ] } 

I checked the validity of the json-string - valid. I try to get access to the values ​​of the fields - there are no errors, which means there is access to the values. But for some reason these same values ​​are not displayed.

 for (var p in json.offers.offer) { if (i == 9) break; i++; sps.append('<div class="col-lg-4 col-sm-6 col-xs-12">' + '<a class="special-card-link" title="' + json.offers.offer[p].attr.title.value + '" href="' + json.offers.offer[p].attr.link.value + '" target="_blank">' + '<div class="card offer">' + '<div class="card-content">' + '<img class="special-offer-card-image" src="//pics.avs.io/180/50/' + json.offers.offer[p].attr.airline_code.value + '.png" alt="' + json.offers.offer[p].attr.airline.value + '"/>' + '<div class="flight-detail">' + '<h6>' + json.offers.offer[p].attr.from_name.value + '</h6>' + '<h6>' + json.offers.offer[p].attr.to_name.value + '</h6>' + '</div>' ); } 

What to do with it?

  • You basically do not use the debugger? - Alexey Shimansky
  • @ Alexey Shimansky what debugger? - JamesJGoodwin
  • Well, programmers use such a magic thing as debugging. It helps to identify errors in the code, especially in hundreds of classes and tens of thousands of lines of code .... But everyone does not sit with a tambourine or do not sing shaman songs, so that everything works and they don’t throw the code on SO) - Alexei Shimansky
  • 2
    @JamesJGoodwin developer tools in the browser can do much more than console.log, it is useful to learn how to use their tools) Well, at least at the breakpoint level to set, lookup variables, see, execute a command on the section with breakpoint. - Duck Learns to Hide
  • one
    Console.log is not debugging ........ well, it's hard to call it debugging ...... read at least learn.javascript.ru/debugging-chrome and generally what browser devtools can do and not just the browser. ..for debugging is carried out on all the YAP, which means not only in the browser - Alexey Shimansky

1 answer 1

You have what offers that offer - arrays. This means you have to either go over the json.offers cycle, and inside the offer cycle, or, if you always have only one element in the offers , then json.offers[0].offer over the json.offers[0].offer and work with the zero element.

If in a very crooked case, then your thing will look something like this:

 var json = { "offers":[ { "offer":[ { "attr":{ "airline":{ "value":"Аэрофлот" }, "airline_code":{ "value":"SU" }, "title":{ "value":"Во Францию от 18662 рублей! Специальное предложение от авиакомпании Аэрофлот" }, "id":{ "value":22098 }, "href":{ "value":"https://www.aviasales.ru/offers/vo-frantsiu-ot-18662-rublei-spetsialnoe-predlozhenie-ot-aviakompanii-aeroflot" }, "sale_date_begin":{ "value":1483488000 }, "sale_date_end":{ "value":1484092800 }, "flight_date_begin":{ "value":1483488000 }, "flight_date_end":{ "value":1490400000 }, "link":{ "value":"https://hydra.aviasales.ru/adaptors/special_offer?iata=SU&utm_source=www&locale=ru&url=http%3A%2F%2Fwww.aeroflot.ru%2Fru-ru%2Fspecial_offers%2F" } }, "description":[ { "text":[ null ] } ], "conditions":[ { "text":"<ul><li>Цены на авиабилеты указаны с учетом топливных, аэропортовых и государственных сборов.</li><li>Количество авиабилетов на каждом рейсе по данному тарифу ограничено.</li><li>Полные условия продажи авиабилетов на сайте авиакомпании.</li></ul>" } ], "route":[ { "attr":{ "from_iata":{ "value":"MOW" }, "to_iata":{ "value":"PAR" }, "from_name":{ "value":"Москва" }, "to_name":{ "value":"Париж" }, "class":{ "value":"эконом" }, "oneway_price":{ "value":null }, "roundtrip_price":{ "value":"от 18662 рублей" } }, "text":[ null ] } ] } ] } ] }; var i = 0; var sps = $('#sps'); var offer = json.offers[0].offer; for (var p in offer) { if (i == 9) break; i++; sps.append('<div class="col-lg-4 col-sm-6 col-xs-12">' + '<a class="special-card-link" title="' + offer[p].attr.title.value + '" href="' + offer[p].attr.link.value + '" target="_blank">' + '<div class="card offer">' + '<div class="card-content">' + '<img class="special-offer-card-image" src="//pics.avs.io/180/50/' + offer[p].attr.airline_code.value + '.png" alt="' + offer[p].attr.airline.value + '"/>' + '<div class="flight-detail">' + '<h6>' + offer[p].route[0].attr.from_name.value + '</h6>' + '<h6>' + offer[p].route[0].attr.to_name.value + '</h6>' + '</div>' ); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="sps"></div> 

  • If you do this, then errors begin to appear. Uncaught TypeError: Cannot read property '0' of undefined - JamesJGoodwin
  • I know nothing, everything works. you apparently did not do it the way it is written in my code for I podredachil it to the right ..... and yes ..... to understand where it jumps out will help you, guess ...... debugger)) - Alexey Shimansky
  • @JamesJGoodwin specially inserted into the snippet, to confirm the words - Alexey Shimansky
  • Yes indeed. I missed something. Thanks for the tips with the debugger! I did not know that the browser can do that. - JamesJGoodwin
  • @JamesJGoodwin there is still a lot of things. but debugger is one main tool. read. sooooo useful - Alexey Shimansky