Parse a complex array using GetJSON jQuery

TL; DR:

  • Started with this question simplified it after he worked and continued it here.
  • I need a "GET" JSON array
  • Format it correctly and for each in the array put it in the corresponding DIV.
  • ??
  • He works.

This is a continuation of this question for simplification and continuation.

I need some complex JSON data from an array. With him, I need a headline and its value. The reason I do this is because I will know what is called an array, but not the data that is generated inside it.

Suppose this new array looks like this:

    {"Days":
[{"day": "Sunday", "time": "10.00"},
 {"day": "Monday", "time": "12.00"},
 {"day": "Tuesday", "time": "09.00"},
 {"day": "Wednesday", "time": "10.00"},
 {"day": "Thursday", "time": "02.00"},
 {"day": "Friday", "time": "05.00"},
 {"day": "Saturday", "time": "08.00"}
]}

Thanks Thanks (Matthew Flaschen)

, 10.00, .

:

$.getJSON(url,
    function(data){
      $.each(data, function(i,item){
      $('.testfield').append('<p>' + item + '</p>');
      });
    });

:

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

, Firebug, JSON. , - . , , ,

$('.testfield').append('<p>' + item + '</p>' + '<br />');

, .

  • ?
  • $getjson div?
+1
3

( JSON - JSON JSONLint):

{
    "Days": [
        {
            "Sunday": "10.00",
            "Monday": "12.00",
            "Tuesday": "09.00",
            "Wednesday": "10.00",
            "Thursday": "02.00",
            "Friday": "05.00",
            "Saturday": "08.00" 
        }
    ]
}

Script :

 $.getJSON( url, function(data){
  $.each(data.Days[0], function(key,value){
   $('.testfield').append('<p>' + key + ' : ' + value + '</p>');
  });
 });
+2

JSON. - :

{"Days":
[{"day": "Sunday", "time": "10.00"},
 {"day": "Monday", "time": "12.00"},
 {"day": "Tuesday", "time": "09.00"},
 {"day": "Wednesday", "time": "10.00"},
 {"day": "Thursday", "time": "02.00"},
 {"day": "Friday", "time": "05.00"},
 {"day": "Saturday", "time": "08.00"}
]}

JSONLint , .

+1

, , DOM. XSS.

, , , "Foo Bar < foo.bar@example.com > " , () .

, jQuery text():

$('.testfield').append($('<p></p>').text(item));
0

Source: https://habr.com/ru/post/1743130/


All Articles