【问题标题】:Handling web service response with multidimensional array javascript使用多维数组 javascript 处理 Web 服务响应
【发布时间】:2015-10-22 07:49:49
【问题描述】:

我正在使用具有多维数组作为响应的 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 () {

                },
            });
        }

请提出建议。

【问题讨论】:

  • 返回的是什么多维数组,在哪里?您的问题是说您使用的 API 返回 JSON?你能解释一下吗
  • records 是一个对象,而不是一个数组..
  • 如果在成功回调的顶部使用“console.log(response)”会得到什么?

标签: javascript php arrays web-services multidimensional-array


【解决方案1】:

您可以使用 for-in 循环遍历 object。确保您获得的密钥是 object 的实际 property

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]);
  });
}

【讨论】:

  • 我得到的输出为 [object,object] 但我在哪里用我的实际属性替换密钥
  • 像这样:console.log(obj.records[key]['destination']);obj.records[key] 将返回您的对象。
猜你喜欢
  • 1970-01-01
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多