簡體   English   中英

有沒有辦法在 Node.JS/JavaScript 中解析 JSON 中的對象的對象

[英]Is there a way to parse a object of an object in JSON in Node.JS/JavaScript

我有一個來自 API 的 JSON 文件需要解析,但是,我需要獲取expHistory的值。 我試過使用雙重解析,但所做的只是返回一個錯誤。 JSON 文件的示例是:

"members": [{
    "uuid": "7a6885b0594148a0be32f713aa1e34b5",
    "rank": "Guild Master",
    "joined": 1516748711264,
    "quest_participation": 1632,
    "exp_history": {
      "2020-10-14": 0,
      "2020-10-13": 35262,
      "2020-10-12": 21614,
      "2020-10-11": 32067,
      "2020-10-10": 0,
      "2020-10-09": 14544,
      "2020-10-08": 4095
    },
    "muted_till": null
  },

如果您的數據在解析原始 JSON 后如下所示:

let data = {
    "members": [{
        "uuid": "7a6885b0594148a0be32f713aa1e34b5",
        "rank": "Guild Master",
        "joined": 1516748711264,
        "quest_participation": 1632,
        "exp_history": {
            "2020-10-14": 0,
            "2020-10-13": 35262,
            "2020-10-12": 21614,
            "2020-10-11": 32067,
            "2020-10-10": 0,
            "2020-10-09": 14544,
            "2020-10-08": 4095
        },
        "muted_till": null
    }]
};

然后,您可以像這樣訪問exp_history數據:

let history = data.members[0].exp_history;
for (const [date, value] of Object.entries(history)) {
   console.log(`${date}: ${value}`);
}

這是一個工作片段:

 let data = { "members": [{ "uuid": "7a6885b0594148a0be32f713aa1e34b5", "rank": "Guild Master", "joined": 1516748711264, "quest_participation": 1632, "exp_history": { "2020-10-14": 0, "2020-10-13": 35262, "2020-10-12": 21614, "2020-10-11": 32067, "2020-10-10": 0, "2020-10-09": 14544, "2020-10-08": 4095 }, "muted_till": null }] }; let history = data.members[0].exp_history; for (const [date, value] of Object.entries(history)) { console.log(`${date}: ${value}`); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM