jQuery 获取 JSON 响应的总数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11257855/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:24:26  来源:igfitidea点击:

Getting total count of JSON response

jqueryjson

提问by Control Freak

Can this be done quickly in jQuery before I start looping through them? Or would I need to also setup a separate group in the response of data with the total count as lets say.. the first variable?

在我开始遍历它们之前,这可以在 jQuery 中快速完成吗?或者我是否还需要在数据响应中设置一个单独的组,总计数可以说..第一个变量?

The common response looks something like this:

常见的响应如下所示:

 [
   {"name":"Tu\u011Frul","surname":"Topuz","message":"Hello World"}
   ,{"name":"Tu\u011Frul","surname":"Topuz","message":"Hello World"}
 ]

But would I use jQuery to return 2as the total count (before I loop through them all?) Or would I need to do something like this?

但是我会使用 jQuery2作为总计数返回(在我遍历它们之前?)还是我需要做这样的事情?

 [
   {"total":"2"}
   ,{"name":"Tu\u011Frul","surname":"Topuz","message":"Hello World"}
   ,{"name":"Tu\u011Frul","surname":"Topuz","message":"Hello World"}
 ]

Whats the most efficient/common (optimization-wise) way of doing this?

什么是最有效/最常见(优化方面)的方法?

回答by Prasenjit Kumar Nag

As after json decoding you are getting an array of objects(from what you have posted). You can always access the lengthproperty of that to get total count.

在 json 解码后,您将获得一组对象(来自您发布的内容)。您始终可以访问该length属性以获取总数。

As you have got this array, I assume you have been able to parse the json response into a JS object. You can use dataType: 'json'to tell jQuery to do that automatically for you or you can parse that yourself using jQuery.parseJSON()but that is necessary if you set dataType:'json'or set content-Typeheader to application/jsonfrom server.

当您获得这个数组时,我假设您已经能够将 json 响应解析为一个 JS 对象。您可以使用dataType: 'json'告诉 jQuery 自动为您执行此操作,或者您可以自己使用解析它,jQuery.parseJSON()但如果您dataType:'json'content-Type标头设置或设置为application/json来自服务器,则这是必要的。

So you can do something like this

所以你可以做这样的事情

$.ajax({
      ....
      dataType: 'json',
      success(function(data){
         if(data){
          var total = data.length; 
         }
      }
 }); 

So you dont need to send the total countback from server. You can get that with JS after getting the response and that will make it easier for you traverse that response using $.eachas the whole array contains same type of objects (doesn't have any special {"total":"2"}member).

所以你不需要total count从服务器发回。您可以在获得响应后使用 JS 获得它,这将使您更轻松地遍历该响应,$.each因为整个数组包含相同类型的对象(没有任何特殊{"total":"2"}成员)。

Note:The lengthproperty is Pure vanilla JS, it has nothing to do with jQuery.

注意:length属性是Pure vanilla JS,与.js 无关jQuery

回答by Denys Séguret

In javascript, you don't manipulate JSON (which is a data exchange standard) but javascript objects.

在 javascript 中,您不操作 JSON(这是一种数据交换标准),而是操作 javascript 对象。

You usually get this object using

您通常使用

var data = JSON.parse(mystring)

or simply because your datais the argument of the success function of a jquery ajax call :

或者仅仅因为您data是 jquery ajax 调用的成功函数的参数:

   $.ajax(
      ....
      dataType:'json',
      success(function(data){
      }) 

Once parsed, what you have is a javascript object.

解析后,您拥有的是一个 javascript 对象。

In your case it's an array, so you can do standard manipulations :

在您的情况下,它是一个数组,因此您可以进行标准操作:

var length = data.length;
var secondObject = data[1];
var firstObjectName = data[0].name;
///etc.