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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效