weixin_33688840 2014-12-19 22:09 采纳率: 0%
浏览 31

使用jQuery遍历JSON

I have a standard AJAX request:

$.ajax({
  type: "POST",
  contentType: "application/json;",
  ...,
  success: function(data){

    // Wanna loop the resultList here...

  }
});

...which returns a JSON object which looks like this:

{
   "notification": "Search complete",
   "resultList": [
      {
         "id": 1,
         "comment": "lorem"
      },
      {
         "id": 2,
         "comment": "ipsim"
      },
      {
         "id": 3,
         "comment": "dolor"
      }
   ]
}

How do I loop though this with jQuery so that the following is printed in the log:
ID #1 - lorem
ID #2 - ipsum
ID #3 - dolor

Sorry for posting another one of these but I can't figure out how to print this without including the notification. Hopefully this will help others as well..

  • 写回答

2条回答 默认 最新

  • weixin_33726943 2014-12-19 22:11
    关注

    Pretty simple, you have an array of objects:

    for (var i = 0; i < data.resultList.length; i++) {
        console.log(data.resultList[i].id);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面