【问题标题】:How to count json object in javascript [duplicate]如何在javascript中计算json对象[重复]
【发布时间】:2016-01-10 20:48:19
【问题描述】:

我如何计算我的请求的对象?

我正在使用 ajax 并向这个 url pbxApi+"/conference/participants/"+circle+"/"+data.conference+"/"+data.uniqueid+'?jsonp=response'; 请求 json 数据,我想计算响应的对象。

这是我的代码

 var uri = pbxApi+"/conference/participants/"+circle+"/"+data.conference+"/"+data.uniqueid+'?jsonp=response';
        getJsonData(uri, function(res){
            console.log(res.length);
});

这是我的功能:

  var getJsonData = function(uri,callback){
    $.ajax({
      type: "GET",
      dataType: "jsonp",
      url: uri,
      jsonpCallback: 'response',
      cache: false,
      contentType: "application/json",
      success: function(json){
        callback(json);
      }
    });
  }

这是我的回应

response({"_id":"561713a78693609968e3bbdd","event":"ConfbridgeJoin","channel":"SIP/192.168.236.15-00000024","uniqueid":"1444352918.94","conference":"0090000293","calleridnum":"0090000288","calleridname":"0090000288","__v":0,"status":false,"sipSetting":{"accountcode":"0302130000","accountcode_naisen":"201","extentype":0,"extenrealname":"UID1","name":"0090000288","secret":"Myojyo42_f","username":"0090000288","context":"innercall_xdigit","gid":101,"cid":"0090000018"}})

谢谢:)

【问题讨论】:

  • “计算对象”是什么意思?
  • 回复中没有json的“结构”很难回答
  • 我添加了我的回复@Anonymous0day
  • 目前还不清楚“计算对象”是什么意思。一个对象是一个单一的东西。所以它是“1”。
  • 我假设您的意思是“计算属性”,这是重复的。如果不是,请澄清您的问题。

标签: javascript json ajax


【解决方案1】:

你可以试试这个:

Object.keys(jsonArray).length;

获取 JSON 对象中的项目数。

另请参阅Object.keys

Object.keys() 返回一个元素为字符串的数组 对应于直接在对象上找到的可枚举属性。 属性的顺序与循环给出的顺序相同 手动覆盖对象的属性。

【讨论】:

    【解决方案2】:

    你可以

    success: function(json){
        console.log('Object keys length: ' + Object.keys(json).length)
        callback(json);
    }
    

    例如{a:1, b:2, c:'Batman'} 给出3 作为答案

    【讨论】:

      【解决方案3】:

      一个解决方案

       var uri = pbxApi+"/conference/participants/"+circle+"/"+data.conference+"/"+data.uniqueid+'?jsonp=response';
      
      var getJsonData = function(uri,callback){
          return $.ajax({ // <----- note the return !
            type: "GET",
            dataType: "jsonp",
            url: uri,
            jsonpCallback: 'response',
            cache: false,
            contentType: "application/json",
            success: function(json){
              if(callback) callback(json);
            }
          });
        }
      
      getJsonData(uri, function(res){
        console.log( Objec.keys(res).length) );
      });
      
      // with the return you will be able to do :
      getJsonData(uri)
        .done( function(res){
          console.log( Objec.keys(res).length) );
        })
        .error(function( err ){
           console.log('an error ?? what is it possible ?');  
        });
      

      【讨论】:

      • return 有什么这么重要?你甚至没有使用它。
      • 无回调链接,我会更新我的答案来理解
      • 这似乎与问题无关。
      • @FelixKling 你说得对!这是一个加号! :-)
      猜你喜欢
      • 2013-06-03
      • 2014-02-24
      • 2018-03-01
      • 1970-01-01
      • 2023-04-05
      • 2014-01-15
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多