Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ajax上检索json数组并循环json数组_Javascript_Php_Arrays_Ajax_Json - Fatal编程技术网

Javascript 在ajax上检索json数组并循环json数组

Javascript 在ajax上检索json数组并循环json数组,javascript,php,arrays,ajax,json,Javascript,Php,Arrays,Ajax,Json,我在ajax上获得了成功。我只是在我的控制器类上对它进行编码,然后在成功时得到它。 这是我的json var info = { "full_name" : "Ray Villalobos", "title" : "Staff Author", "links" : [ { "blog" : "http://example.com", "facebook" : "http://facebook.com/ex

我在ajax上获得了成功。我只是在我的控制器类上对它进行编码,然后在成功时得到它。 这是我的json

var info = {
    "full_name" : "Ray Villalobos",
    "title" : "Staff Author",
    "links" : [
       {
            "blog"     : "http://example.com",
            "facebook" : "http://facebook.com/example",
            "youtube"  : "http://www.youtube.com/example",
            "podcast"  : "http://feeds.feedburner.com/example",
            "twitter"  : "http://twitter.com/example" 
       },
       {
            "blog"     : "http://example.com",
            "facebook" : "http://facebook.com/example",
            "youtube"  : "http://www.youtube.com/example",
            "podcast"  : "http://feeds.feedburner.com/example",
            "twitter"  : "http://twitter.com/example" 
        },
        {
            "blog"     : "http://example.com",
            "facebook" : "http://facebook.com/example",
            "youtube"  : "http://www.youtube.com/example",
            "podcast"  : "http://feeds.feedburner.com/example",
            "twitter"  : "http://twitter.com/example" 
        }
    ]
};
现在,如果我的链接只有一个数据集而不是一个数组,那么我正在像这样检索键和值:

for ( key in info.links ) {
    if (info.links.hasOwnProperty(key)) {
    output += '<li>' +
    '<a href = "' + info.links[key] + '">' + key + '</a>' + '</li>';
   } //if the links has the key property
} // for...go through each link
for(输入信息链接){
if(info.links.hasOwnProperty(键)){
输出+='
  • '+ “+”
  • ”; }//如果链接具有key属性 }//对于…遍历每个链接

    如何在我的循环中检索数组键和值?

    我不确定您在这里要做什么,但在“for…in”循环中,这里的键是数组的索引。您可以使用以下代码获取链接:

    var link = info.links[key];
    
    然后检索相关属性,在您的情况下,属性可以是“link.blog”或“link.facebook”等。

    for(info.links中的infolink){
    用于(输入信息链接){
    if(infolink.hasOwnProperty(键)){
    输出+='
  • '+ “+”
  • ”; }//如果链接具有key属性 }//对于…遍历每个链接 }


    此处数组信息链接的元素不是数组。info.links的值是对象,因此您无法像那样检索“blog/facebook/youtube”。键的输出将为“0/1/2”

    所以你可以试试这个

        for(var key in info.links){
            document.write('<li><a href="'+info.links[key].blog+'">Blog</a></li>');
            document.write('<li><a href="'+info.links[key].facebook+'">Facebook</a></li>');
            document.write('<li><a href="'+info.links[key].youtube+'">YouTube</a></li>');
            document.write('<li><a href="'+info.links[key].podcast+'">Podcast</a></li>');
            document.write('<li><a href="'+info.links[key].twitter+'">twitter</a></li>');
            document.write('<hr/>');
    }
    
    for(信息链接中的变量键){
    文件。写(“
  • ”); 文件。写(“
  • ”); 文件。写(“
  • ”); 文件。写(“
  • ”); 文件。写(“
  • ”); 文件。写(“
    ”); }
    终于达到了我的预期。代码如下:

    var output = "";
    var linksCount = info.links;
    
    for (var counter = 0; counter < linksCount.length; counter++) {
        for ( key in infolink[counter]) {
            if (infolink[counter].hasOwnProperty(key)) {
                output += '<li>' +
                '<a href = "' + infolink[key][counter] + '">' + key[counter] + '</a>' + '</li>';                            
            } //if the links has the key property
        } // for...go through each link
    
    var输出=”;
    var linkscont=info.links;
    对于(变量计数器=0;计数器”;
    }//如果链接具有key属性
    }//对于…遍历每个链接
    

    }

    您是否尝试过用foreach($key=>arrayValue)替换所有信息。链接到信息。链接[0]希望对您有所帮助。我尝试过这样做。但在这里它只得到链接数组的第一个数组。我想检索链接数组中的所有数组数据。
    var output = "";
    var linksCount = info.links;
    
    for (var counter = 0; counter < linksCount.length; counter++) {
        for ( key in infolink[counter]) {
            if (infolink[counter].hasOwnProperty(key)) {
                output += '<li>' +
                '<a href = "' + infolink[key][counter] + '">' + key[counter] + '</a>' + '</li>';                            
            } //if the links has the key property
        } // for...go through each link