简体   繁体   English

Javascript / Jquery解析Json请求

[英]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"]} 因此,我的页面向php页面发出请求,并且该页面返回带有数组结果的JSON PHP返回示例: {"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: 每个名称,位置(json数组?)都有多个值,我该如何像使用此代码一样解析非常大的值:

    $('#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" 有了这个jQuery类型Zizi,Zizi,我该如何准备或计算“名称”中的值

.length is properties of array that gives you their count. .length是数组的属性,可为您提供计数。

Use that like this: data.name.length; 像这样使用: data.name.length;

Also, getJSON will only give you JSON string. 另外, getJSON仅会提供JSON字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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