使用Javascript读取JSON对象内的Array内的对象

时间:2018-04-28 20:34:45

标签: javascript json

在提供的编码JSON对象中,存储我想要读取Id / factorType / provider的值的对象数组。

{"factors":{
    "factors":[{
    "id":"reytfufsiwdg",
    "factorType":"email",
    "provider":"ABC",
    "factorsProfile":{
    "email":"a...r@gmail.com"
    }}]}}

2 个答案:

答案 0 :(得分:0)

这样就可以了:

data.factors.factors.forEach(factor => {
    console.log(`ID is: ${factor.id}`)
  console.log(`Type is ${factor.factorType}`)
  console.log(`Provider is: ${factor.provider}`)
})

您甚至可以在此处尝试:https://jsfiddle.net/qe4a4Lmg/

答案 1 :(得分:0)



var data = {"factors":{
    "factors":[{
    "id":"reytfufsiwdg",
    "factorType":"email",
    "provider":"ABC",
    "factorsProfile":{
    "email":"a...r@gmail.com"
    }}]}};
    

    
   for(var key in data){
    for(var innerKey in data[key]){
     for(var innerKey2 in data[key][innerKey]){
        for(var innerKey3 in data[key][innerKey][innerKey2]){
          if(innerKey3 == "factorType" || innerKey3 == "provider"|| innerKey3 == "id")
         console.log(data[key][innerKey][innerKey2][innerKey3])
        }
      }
    }
  }




或者您可以使用



var data = {"factors":{
        "factors":[{
        "id":"reytfufsiwdg",
        "factorType":"email",
        "provider":"ABC",
        "factorsProfile":{
        "email":"a...r@gmail.com"
        }}]}};
        


data.factors.factors.forEach(factor => {
  console.log(factor.id)
    console.log(factor.factorType)
      console.log(factor.provider)
});