简体   繁体   中英

Javascript/Jquery Parse Json request

So my page make a request to a php page and that page return a JSON with an array result PHP Return Example: {"name":["Zizi","Zizi"],"position":["86","86"],"points":["26","26"]}

Every name, position (json array ?) have multiple values how can I parse very value like I'm using this code:

    $('#getdata-button').live('click', function(){
    $.getJSON('all.php', function(data) {
        $('#showdata').html("<p>item1="+ data.name +"</p>");*/
    });
});

With this the jQuery type Zizi,Zizi, How can i ready or count the values inside the "name"

.length is properties of array that gives you their count.

Use that like this: data.name.length;

Also, getJSON will only give you JSON string.

You've to parse it.

Do this:

$.getJSON('all.php', function(data) {
      data = JSON.parse(data);
      var length=data.name.length;
      $('#showdata').html("<p>item1="+ data.name +"</p>");*/
});

Found a solution I do not know if it's the most correct

    $('#getdata-button').live('click', function(){
    $.getJSON('all.php', function(data) {
      $(data.name).each(function(index){
                 $('#showdata').append('<p>Name = ' + data.name[index] +' Position= ' + data.position[index] + ' Points = ' + data.points[index] + '</p>');     
            });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM