Good day. On json request, I get this answer

data:Array[423] [0 … 99] 0:Object CityDescription:"Київ" CityDescriptionRu:"Киев" CityRef:"8d5a980d-391c-11dd-90d9-001a92567626" Delivery:Object Description:"Відділення №1: вул. Пирогівський шлях, 135" DescriptionRu:"Отделение №1: ул. Пироговский путь, 135" Latitude:"50.353444000000000" Longitude:"30.542863000000000" Number:"1" POSTerminal:"0" Phone:"0-800-500-609" PlaceMaxWeightAllowed:0 PostFinance:"1" Reception:Object Ref:"1ec09d88-e1c2-11e3-8c4a-0050568002cf" Schedule:Object SiteKey:"105" TotalMaxWeightAllowed:0 TypeOfWarehouse:"9a68df70-0267-42a8-bb5c-37f427e36ee4" __proto__:Object 

I need a drop-down list to display a drop-down list "DescriptionRu" of all results (423)

I tried this method.

  success: function(response) { console.log(response); var json_obj = $.parseJSON(response);//parse JSON var output="<ul>"; for (var i in json_obj) { output+="<li>" + json_obj[i].DescriptionRu + "</li>"; } output+="</ul>"; $('#content').html(output); } 

  • Run the loop through the array, at each iteration, somewhere you need, add the next element. What is your difficulty? - Ivan Pshenitsyn
  • At the stage of collecting these data and output to the screen. Found examples where enumerate arrays. by analogy, nothing happened with me. - WebMorda
  • 2
    Your answer is returned to data , but you don’t use this word anywhere in your code. - Mr. Brightside

1 answer 1

Probably, you were just slightly mistaken in the data structure: judging by the console.log output that you presented, inside the response, the data has the structure

 { data: [ {CityDescription:"Київ",.....,DescriptionRu:"....",..}, {CityDescription:"Київ",.....,DescriptionRu:"....",..}, {CityDescription:"Київ",.....,DescriptionRu:"....",..}, ... ] } 

Respectively and need to sort through json_obj.data:

 success: function(response) { console.log(response); var output="<ul>"; for (var i = 0; i < response.data.length; i++) { output+="<li>" + response.data[i].DescriptionRu + "</li>"; } output+="</ul>"; $('#content').html(output); } 
  • one
    better do without for...in with arrays - Grundy
  • @Grundy yes, you're right, corrected. I thought, and so a ride, but no, they noticed) - Ivan Pshenitsyn
  • @IvanPshenitsyn, swears at the line "var json_obj = $ .parseJSON (response);" VM759: 1 Uncaught SyntaxError: Unexpected token in JSON at position 1 - WebMorda
  • @WebMorda means you have an incorrect response. output it to the console and check. - Ivan Pshenitsyn
  • one
    Thank you @IvanPshenitsyn. Everything worked out. - WebMorda