2017-07-24 102 views
0

我正在寻找解析json url /文件并仅从json获取顶级节点。如何解析Json对象以获取所需对象

JSON对象看起来是这样的:

{ 
"2017080350": { 
    "home": { 
     "score": { 
      "1": null, 
      "2": null, 
      "3": null, 
      "4": null, 
      "5": null, 
      "T": null 
     }, 
     "abbr": "ARI", 
     "to": null 
    }, 
    "away": { 
     "score": { 
      "1": null, 
      "2": null, 
      "3": null, 
      "4": null, 
      "5": null, 
      "T": null 
     }, 
     "abbr": "DAL", 
     "to": null 
    }, 
    "bp": 0, 
    "down": null, 
    "togo": null, 
    "clock": null, 
    "posteam": null, 
    "note": null, 
    "redzone": null, 
    "stadium": "Tom Benson Hall of Fame Stadium", 
    "media": { 
     "radio": { 
      "home": null, 
      "away": null 
     }, 
     "tv": "NBC", 
     "sat": null, 
     "sathd": null 
    }, 
    "yl": null, 
    "qtr": null 
}, 
"2017080351": { 
    "home": { 
     "score": { 
      "1": null, 
      "2": null, 
      "3": null, 
      "4": null, 
      "5": null, 
      "T": null 
     }, 
     "abbr": "ARI", 
     "to": null 
    }, 
    "away": { 
     "score": { 
      "1": null, 
      "2": null, 
      "3": null, 
      "4": null, 
      "5": null, 
      "T": null 
     }, 
     "abbr": "DAL", 
     "to": null 
    }, 
    "bp": 0, 
    "down": null, 
    "togo": null, 
    "clock": null, 
    "posteam": null, 
    "note": null, 
    "redzone": null, 
    "stadium": "Tom Benson Hall of Fame Stadium", 
    "media": { 
     "radio": { 
      "home": null, 
      "away": null 
     }, 
     "tv": "NBC", 
     "sat": null, 
     "sathd": null 
    }, 
    "yl": null, 
    "qtr": null 
} 
} 

我试图让是两个值20170803502017080351 我可以读取并显示的内容不错,但我试图当有问题得到两个提到的节点?

+0

使用Object.getOwnPropertyNames(obj)。 – dev8080

+0

您能否在阅读内容时提供代码? – trincot

+0

你想得到这两个键? – 2017-07-24 17:57:03

回答

1

如果你的反应还没有解析,那么首先要做:

response = JSON.parse(response); 

然后你可以使用一个for...in循环:

for (let key in response) { 
    console.log(key); 
} 

要阅读链接到关键数据,你会展开如下:

for (let key in response) { 
    console.log(key); 
    let value = response[key]; 
    // and you can go deeper... 
    for (let deeperKey in value) { 
     // ...etc. 
    } 
}