无法访问 Javascript 中的 JSON 内的嵌套对象

Not able to access nested objects inside a JSON in Javascript

我正在尝试提取 JSON 文件中的嵌套对象。 JSON 文件包含有关印度 statesdistricts 的信息,看起来像这样:- https://api.covid19india.org/state_district_wise.json

{
"Haryana": {
    "districtData": {

      "Ambala": {
        "notes": "",
        "active": 2,
        "confirmed": 14,
        "deceased": 1,
        "recovered": 11,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Bhiwani": {
        "notes": "",
        "active": 1,
        "confirmed": 3,
        "deceased": 0,
        "recovered": 2,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Charkhi Dadri": {
        "notes": "",
        "active": 0,
        "confirmed": 1,
        "deceased": 0,
        "recovered": 1,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Faridabad": {
        "notes": "",
        "active": 18,
        "confirmed": 61,
        "deceased": 1,
        "recovered": 42,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Fatehabad": {
        "notes": "",
        "active": 0,
        "confirmed": 1,
        "deceased": 0,
        "recovered": 1,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Gurugram": {
        "notes": "",
        "active": 19,
        "confirmed": 57,
        "deceased": 0,
        "recovered": 38,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      }
    }

"Maharashtra": {
    "districtData": {
      "Other States": {
        "notes": "Cases from other States/UTs",
        "active": 24,
        "confirmed": 27,
        "deceased": 3,
        "recovered": 0,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Ahmednagar": {
        "notes": "",
        "active": 17,
        "confirmed": 42,
        "deceased": 2,
        "recovered": 23,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Akola": {
        "notes": "",
        "active": 30,
        "confirmed": 39,
        "deceased": 1,
        "recovered": 8,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Amravati": {
        "notes": "",
        "active": 17,
        "confirmed": 28,
        "deceased": 7,
        "recovered": 4,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Aurangabad": {
        "notes": "",
        "active": 131,
        "confirmed": 161,
        "deceased": 8,
        "recovered": 22,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Bhandara": {
        "notes": "",
        "active": 1,
        "confirmed": 1,
        "deceased": 0,
        "recovered": 0,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Beed": {
        "notes": "",
        "active": 0,
        "confirmed": 1,
        "deceased": 0,
        "recovered": 1,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Buldhana": {
        "notes": "Reconciled as per MH bulleting 24/04",
        "active": 1,
        "confirmed": 21,
        "deceased": 1,
        "recovered": 19,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      "Chandrapur": {
        "notes": "",
        "active": 1,
        "confirmed": 3,
        "deceased": 0,
        "recovered": 2,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      }
   }
}

以上信息表示嵌套在状态对象中的区数据对象(这里的区相当于一个城市)。例如,Haryana 是州,Ambala 是哈里亚纳邦的一个地区。所以有多个州,每个州内有多个地区。我想访问地区内的 activedeceased 对象,并创建印度所有地区对象的数组,其中包含有关所有地区的信息看起来像这样:-

[
  {
   "active":
   "deceased":
   "recovered":
   "districtname":"District-A"
   "statename":
  },
  {
   "active":
   "deceased":
   "recovered":
   "districtname":"District-B"
   "statename":
  },
  {
   "active":
   "deceased":
   "recovered":
   "districtname":"District-C"
   "statename":
  },
  {
   "active":
   "deceased":
   "recovered":
   "districtname":"District-D"
   "statename":
  }
]

我尝试了 for...in 方法,也尝试将内部对象转换为数组,但无法正确循环。谁能建议怎么做?

这是循环遍历对象的一种方法,但我不确定它是否是最有效的方法。

const obj = {

"a": {
    
    "b":{
        "b1":1,
        "b2":2,
    },
    "c":{
      "c1":3,
      "c2":4,
    }

  } 

}


for(let key in obj){
  let val = obj[key]
  for(let k in val){
  
    let  v = val[k]
    for (let k2 in v){
      console.log(k2, v[k2])
    }
    
  }

}

尝试使用 for 循环和 Object.values()

for (let value of Object.values(obj)) {
  console.log(value);
}

obj是你的数据。

我创建了以下示例。它将创建一个对象数组,其中包含 州名称地区名称活动密钥和增量值。

我正在使用 object.entries 其中 returns 包含键值对的数组数组。本质上是一个元组数组。我将其与对象解构和扩展运算符结合起来。

const transformFlat = (obj) => {
  const result = [];
  Object.entries(obj).forEach(([stateName, { districtData }]) =>
    Object.entries(districtData).forEach(([districtName, { active, delta }]) =>
      result.push({
        state: stateName,
        district: districtName,
        active,
        ...delta,
      })
    )
  );
  return result;
};

console.log(transformFlat({
  Haryana: {
    districtData: {
      Ambala: {
        notes: '',
        active: 2,
        confirmed: 14,
        deceased: 1,
        recovered: 11,
        delta: {
          confirmed: 0,
          deceased: 0,
          recovered: 0,
        },
      },
      Bhiwani: {
        notes: '',
        active: 1,
        confirmed: 3,
        deceased: 0,
        recovered: 2,
        delta: {
          confirmed: 0,
          deceased: 0,
          recovered: 0,
        },
      },
    },
  },
  Maharashtra: {
    districtData: {
      'Other States': {
        notes: 'Cases from other States/UTs',
        active: 24,
        confirmed: 27,
        deceased: 3,
        recovered: 0,
        delta: {
          confirmed: 0,
          deceased: 0,
          recovered: 0,
        },
      },
      'Ahmednagar': {
        notes: '',
        active: 17,
        confirmed: 42,
        deceased: 2,
        recovered: 23,
        delta: {
          confirmed: 0,
          deceased: 0,
          recovered: 0,
        },
      },
    },
  },
}))