1

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"

0

2 Answers 2

1

.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>");*/
});
Sign up to request clarification or add additional context in comments.

2 Comments

data is an object, using data.name accesses the value of the name key in the object and name.length returns the length of the value which is an array.
data.name.length return me undefined X:
0

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>');     
            });
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.