如何在多个级别上迭代JSON? [英] How to iterate JSON with multiple levels?

查看:120
本文介绍了如何在多个级别上迭代JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欢迎,我遇到了JSON文件问题.我想遍历所有状态并将所有状态放入数组,但是我陷入了困境.我加载并解析它.然后,我尝试使用forEach,但没有成功.感谢您的帮助!

Welcome, I got a problem with JSON file. I would like to iterate through it and get every status into array, but I got stuck. I load this file and parse it. Then I tried to use forEach but it did not worked. Thanks for help!

[{
  "offers": [{
    "advertiser_api_id": 12,
    "status": 1
  }, {
    "advertiser_api_id": 13,
    "status": 0
  }]
}]

推荐答案

这将遍历对象并将数据打印到控制台-您可能未到达要遍历的正确数组.

This loops through the object and prints the data to console - you likely did not reach the correct array to loop through.

var data = [{
  "offers": [{
    "advertiser_api_id": 12,
    "status": 1
      }, {
    "advertiser_api_id": 13,
    "status": 0
  }]
}];

data[0].offers.forEach(function(element) {
  console.log(element.status);
});

这篇关于如何在多个级别上迭代JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆