Getjson jquery array parsing

The resulting simplified array works below

Following a complex array for parsing, see here .


TL; DR . You want to get each title from an array and paste it into a div, not knowing what is inside the div using jQuery - getJSON.

Change . The data comes from a piece of software that outputs a JSON string every couple of seconds with new data, so I need to output the data array to "d", as in the example below. So I have to get "Title" and "034324324" etc. For each.

Edit2 Updated code that exactly matches me.

I have a JSON array that allows me to say:

{
"Days":
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
}

, "", div.

function getdata() {
31 $.ajaxSetup ({ cache: false}); //<!-- Stops IE from caching data //-->
32 
33 $.getJSON(testurl, function(data) {
34 $.each(data.days, function(i,item){
35 $('#testfield').append('<p>' + item + '</p>');
36
37 });
38 }); 

.

Getjson , , , . :

$('#name').html(data.Projectname);

, , , . , - , ^^.

JSON, , :

{"Projectname":"ertyofhj","Projectid":"aqwertyuqw","Projecttitle":"ertyofhj"}

, , .

. firebug IE (Aargh)

33 $.getJSON(testurl, function(data) {

38 });

34 $.each(data.days, function(i,item){

- GET JSON. $.each, . Firebug , JSON?

JSON - . ( ), , . - . . , ( ) , , , "+" < br/" > , . , :

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

Dayname .

+3
3
$.getJSON(url,
    function(data){
      $.each(data.items, function(i,item){
      $('#testfield').html('<p>' + item.d.title + '</p>');
      });
    });

HTML- "testfield" ... jQuery.append, ,

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

JSON : "d" . , , ?

+1

,


<html>
<head>
  <title>Stackoverflow</title>
  // ** POINT THIS TO A VALID jquery.js LOCATION
  <script src="jquery.js" type="text/javascript"></script>
  <script>
    var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
    // *****************************************************
    // This is your data from getJSON if correctly formatted
    var data = '{"Days":[ {"Sunday":"10.00"}, {"Monday":"12.00"}, {"Tuesday":"09.00"}, {"Wednesday":"10.00"}, {"Thursday":"02.00"}, {"Friday":"05.00"}, {"Saturday":"08.00"}]}';
    runUpdate = function() {
      // *******************************************************
      // Clear the div of all data by replacing it
      $('#testfield').replaceWith('<div id="testfield"></div>');
      var dataObject= $.parseJSON( data );
      $.each( dataObject.Days, function (index, value) {
        // ********************************************
        // Append new elements to the newly created div
        $('#testfield').append("<p>"+days[index]+": "+value[ days[index] ]+"</p>");
      });
    };

    runReset = function() {
      $('#testfield').replaceWith('<div id="testfield">This is original text.</div>');
    };
  </script>
</head>
<body>
  <div id="testfield">
    This is original text.
  </div>
  <div>
    <input type="button" value="Run Update" onclick="runUpdate();" />
    <input type="button" value="Reset" onclick="runReset();" />
  </div>
</body>
</html>
+1

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


All Articles