javascript - 使用多维数组 javascript 处理 Web 服务响应

标签 javascript php arrays web-services multidimensional-array

我正在使用一个具有多维数组响应的 Web 服务,当我警告元素时,我得到未定义的输出,请参阅下面的代码。

JSON 响应

{  
   "status":1,
   "msg":"cc call history",
   "records":{  
      "1":{  
         "destination":"Canada - Fixed Others",
         "date":"May 05, 2010",
         "time_spent":"1 minutes",
         "amount_charged":"2.18"
      },
      "2":{  
         "destination":"Canada - Fixed Others",
         "date":"May 05, 2010",
         "time_spent":"1 minutes",
         "amount_charged":"2.18"
      }
   }
}

Javascript代码

function call()
        {
            alert('call');
            var user_id = sessionStorage.getItem('user_id');
            var authentication_key = sessionStorage.getItem('auth_id');
            alert(user_id);
            alert(authentication_key);
            $.ajax({
                type: 'GET',
                url: 'http://example.com/XXX',
                data: {user_id: user_id, authentication_key: authentication_key},
                success: function (response)
                {
                    obj = JSON.parse(response);
                    var status = obj.status;
                    var msg = obj.msg;
                    var records;

                    alert(status);
                    alert(msg);
                    alert(records);
                    if (status === '0')
                    {
                        alert(status);
                    }
                    else
                    {
                        var lnrc = obj.records.length;
                        alert(lnrc);
                        for (var i = 0; i < 2; i++)
                        {
                            alert('for');
                            var rc1 = obj.records[i];
                            alert(rc1);
                        }
                    }
                },
                error: function () {

                },
            });
        }

请提出一些建议。

最佳答案

You can use the for-in loop to iterate through object. Make sure that the key you get is an actual property of an object

var obj = {
  "status": 1,
  "msg": "cc call history",
  "records": {
    "1": {
      "destination": "Canada - Fixed Others",
      "date": "May 05, 2010",
      "time_spent": "1 minutes",
      "amount_charged": "2.18"
    },
    "2": {
      "destination": "Canada - Fixed Others",
      "date": "May 05, 2010",
      "time_spent": "1 minutes",
      "amount_charged": "2.18"
    }
  }
};
var status = obj.status;
var msg = obj.msg;
var records;
if (status === '0') {
  alert(status);
} else {
  for (var key in obj.records) {
    if (obj.records.hasOwnProperty(key)) {
      console.log(obj.records[key]);
    }
  }
  //OR
  Object.keys(obj.records).forEach(function(key) {
  console.log(obj.records[key]);
  });
}

关于javascript - 使用多维数组 javascript 处理 Web 服务响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33275839/

相关文章:

javascript - 等待 Promise.all 中的 array.map 迭代

javascript:获取关联数组中包含重复数据的键

python - 数组中的条件选择

javascript - 为什么我不能使用 JavaScript 隐藏这个 div?

javascript - 在 Dart 中查找位置/坐标

javascript - 响应时字体大小会有所不同

javascript - 如何防止 HTML 表单提交空白

php - 如何让随机图像每次刷新一个样式表,但在选择另一个样式表时消失?

php - 将联系人从 gmail/hotmail/yahoo 导入到 php

arrays - 在 C 中,如何 "directly"将结构复制到数组成员?