簡體   English   中英

如何從Typescript中以#或@開頭的屬性獲取JSON數據

[英]How to get JSON data from attributes that start with # or @ in Typescript

我正在使用一個特定的 API,它返回一個 JSON,看起來像下面的示例。

在此處輸入圖像描述

我想獲取包含 #text 和 @attr 的兩個值,但是當我嘗試獲取這些值時,我在 typescript 中收到錯誤消息。

嘗試使用,

album[0]["@attr"]
album[0]["artist"]["#text"]

嘿,對於 JSON,您可以通過其中的屬性名稱使用獲取詳細信息,並且對於以 @ 或 # 開頭的全天候相同,它將是相同的。

請參閱下面的代碼以獲取指定鍵的值:

樣品 JSON:

{
    "weeklyalbumchart": {
        "album": [
            {
                "artist": {
                    "mbid": "data",
                    "#text": "Flying Lotus"
                },
                "mbid": "data",
                "url": "",
                "name": "",
                "@attr": {
                    "rank": "1"
                },
                "playcount": "21"
            },
            {
                "artist": {
                    "mbid": "data",
                    "#text": "Flying Lotus"
                },
                "mbid": "data",
                "url": "",
                "name": "",
                "@attr": {
                    "rank": "1"
                },
                "playcount": "21"
            }
        ]
    }
}

讀取 JSON:

@attr ===> json["weeklyalbumchart"]["album"][0]["@attr"]

#text ===> json["weeklyalbumchart"]["album"][0]["artist"]["#text"]

希望這能幫助你理解它。

暫無
暫無

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

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