如何使用 JQuery 获取 Json 记录总数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/453209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the total Json record count using JQuery?
提问by Tiago
I couldn't find anything about getting the total JSON record count using jQuery.
我找不到有关使用 jQuery 获取总 JSON 记录数的任何信息。
Here is the JSON returned:
这是返回的 JSON:
{"Email":"Please enter your Email.","Password":"Please enter a password."}
{"Email":"Please enter your Email.","Password":"Please enter a password."}
Here is my code:
这是我的代码:
$(function() {
$("#btnSubmit").click(function() {
$.ajax({
url: "/account/signup",
type: "POST",
dataType: "json",
data: {
Email: $("#strEmail").val(),
Password: $("#strPassword").val()
},
success: function(j) {
$(".errMsg").hide();
alert(j.length); // I couldn't get the total count
$.each(j, function(n) {
$("#err" + n).html(j[n]);
$("#err" + n).show();
})
},
error: function(req, status, error) {
alert(req);
}
});
});
});
回答by Tiago
If you have something like this:
如果你有这样的事情:
var json = [ {a:b, c:d}, {e:f, g:h, ...}, {..}, ... ]
then, you can do:
然后,你可以这样做:
alert(json.length)
回答by TimH
The OP is trying to count the number of properties in a JSON object. This could be done with an incremented temp variable in the iterator, but he seems to want to know the count before the iteration begins. A simple function that meets the need is provided at the bottom of this page.
OP 正在尝试计算 JSON 对象中的属性数量。这可以通过迭代器中增加的临时变量来完成,但他似乎想在迭代开始之前知道计数。本页底部提供了一个满足需要的简单功能。
Here's a cut and paste of the code, which worked for me:
这是代码的剪切和粘贴,对我有用:
function countProperties(obj) {
var prop;
var propCount = 0;
for (prop in obj) {
propCount++;
}
return propCount;
}
This should work well for a JSON object. For other objects, which may derive properties from their prototype chain, you would need to add a hasOwnProperty() test.
这应该适用于 JSON 对象。对于其他可能从其原型链派生属性的对象,您需要添加一个 hasOwnProperty() 测试。
回答by svinto
Why would you want length in this case?
在这种情况下你为什么要长度?
If you do want to check for length, have the server return a JSON array with key-value pairs like this:
如果您确实想检查长度,请让服务器返回一个带有键值对的 JSON 数组,如下所示:
[
{key:value},
{key:value}
]
In JSON, [ and ] represents an array (with a length property), { and } represents a object (without a length property). You can iterate through the members of a object, but you will get functions as well, making a length check of the numbers of members useless except for iterating over them.
在 JSON 中,[ 和 ] 代表一个数组(有长度属性),{ 和 } 代表一个对象(没有长度属性)。您可以遍历对象的成员,但您也将获得函数,除了遍历它们之外,对成员数量进行长度检查是没有用的。
回答by tanathos
Your json isn't an array, it hasn't length property. You must change your data return or the way you get your data count.
您的 json 不是数组,它没有 length 属性。您必须更改数据返回或获取数据计数的方式。
回答by Satish
Try the following:
请尝试以下操作:
var count=Object.keys(result).length;
Does not work IE8 and lower.
不适用于 IE8 及更低版本。
回答by William Kheng
Perhaps you might want to try JSON.parse your return result first then you can get the count by .length
也许您可能想先尝试 JSON.parse 您的返回结果,然后您可以通过 .length 获取计数
回答by frenchmd
What you're looking for is
你要找的是
j.d.length
The dis the key. At least it is in my case, I'm using a .NET webservice.
该d是关键。至少在我的情况下,我使用的是 .NET 网络服务。
$.ajax({
type: "POST",
url: "CantTellU.asmx",
data: "{'userID' : " + parseInt($.query.get('ID')) + " }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg, status) {
ApplyTemplate(msg);
alert(msg.d.length);
}
});