Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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
使用JQuery处理JSON对象_Jquery_Arrays_Json - Fatal编程技术网

使用JQuery处理JSON对象

使用JQuery处理JSON对象,jquery,arrays,json,Jquery,Arrays,Json,您好,我遇到了一个奇怪的问题,我试图获取json对象数组中最后一个元素的id $.ajax ({ type: 'POST', url: 'post-get.php', data: { group: group, postid: postid }, cache: false, dataType: 'json', success: function(response) { if (resp

您好,我遇到了一个奇怪的问题,我试图获取json对象数组中最后一个元素的id

$.ajax ({
    type: 'POST',
    url: 'post-get.php',
    data:
    {
        group: group,
        postid: postid
    },
    cache: false,
    dataType: 'json',
    success: function(response) {
        if (response.length > 0)
        {
            $.each(response, function(index, element) {
                var p = "";
                p += "<div class='col-md-12' style='padding:0'>"; // start col-md-12
                p += "<div class='user'>"; // start user
                p += "<div class='pic'>"; // start pic
                p += "<img src='img/timthumb.php?src=";
                p += element[2];
                p += "&w=100&h=100&a=t' />";
                p += "</div>"; // end pic
                p += "<span class='username'>"; // start username
                p += element[1];
                p += "</span>"; // end username
                p += "</div>";// end user
                p += "<div class='content'>"; // start content
                p += element[4];
                p += "</div>"; // end content
                p += "<div class='clear'></div>";
                p += "<div class='info' style='margin-top:20px;'>"; // start info
                p += "<div class='datetime'>Posted on: ";// start datetime
                p += element[5];
                p += "</div>"; // end datetime
                p += "<div class='clear'></div>";
                p += "</div>";  // end info
                p += "</div>"; // end col-md-12
                p += "<div class='clear'></div>";

                $(".post").html($(".post").html() + p + element[6]);
            });
        }
    }
});
你可以用

response[index].propertyname
有关更多详细信息,请参阅


在Jason p的评论之后,我意识到我使用了错误的索引。我已经做了修正,它现在起作用了

尝试像这样访问属性名,因为数据重复了两次,所以可以从服务中删除重复的数据。而且还可以通过不动产名称而不是容易理解的数字进行访问

var p = "";
p += "<div class='col-md-12' style='padding:0'>"; // start col-md-12
p += "<div class='user'>"; // start user
p += "<div class='pic'>"; // start pic
p += "<img src='img/timthumb.php?src=";
p += element["2"];
p += "&w=100&h=100&a=t' />";
p += "</div>"; // end pic
p += "<span class='username'>"; // start username
p += element["1"];
p += "</span>"; // end username
p += "</div>";// end user
p += "<div class='content'>"; // start content
p += element["4"];
p += "</div>"; // end content
p += "<div class='clear'></div>";
p += "<div class='info' style='margin-top:20px;'>"; // start info
p += "<div class='datetime'>Posted on: ";// start datetime
p += element["5"];
p += "</div>"; // end datetime
p += "<div class='clear'></div>";
p += "</div>";  // end info
p += "</div>"; // end col-md-12
p += "<div class='clear'></div>";

$(".post").html($(".post").html() + p + element["6"]);

发布response.show的内容。显示在json中返回的json,键为6的属性的值是8。。对于数组中的每个对象。谢谢。我似乎使用了错误的键,我现在更正了它,它工作了。
var p = "";
p += "<div class='col-md-12' style='padding:0'>"; // start col-md-12
p += "<div class='user'>"; // start user
p += "<div class='pic'>"; // start pic
p += "<img src='img/timthumb.php?src=";
p += element["2"];
p += "&w=100&h=100&a=t' />";
p += "</div>"; // end pic
p += "<span class='username'>"; // start username
p += element["1"];
p += "</span>"; // end username
p += "</div>";// end user
p += "<div class='content'>"; // start content
p += element["4"];
p += "</div>"; // end content
p += "<div class='clear'></div>";
p += "<div class='info' style='margin-top:20px;'>"; // start info
p += "<div class='datetime'>Posted on: ";// start datetime
p += element["5"];
p += "</div>"; // end datetime
p += "<div class='clear'></div>";
p += "</div>";  // end info
p += "</div>"; // end col-md-12
p += "<div class='clear'></div>";

$(".post").html($(".post").html() + p + element["6"]);