簡體   English   中英

php json解析對象

[英]php json parsing objects

我有一個需要解析的JSON文件。 我是php的新手,並試圖弄清楚如何正確解析對象。 我的問題是: How do I parse the objects of an object that have different names

一個對象的名稱為Thresh ,列表中的下一個對象的名稱為Aatrox ,頂級對象的名稱為data

這就是JSON的樣子。 如果我知道對象的名稱,我可以訪問該信息$champion = $jfo->data->Thresh; 但我不想輸入冠軍的所有名字。 是否有一種簡單的方法可以在不知道名稱的情況下獲取所有單獨的對象? Maybe regex?

"data": {
    "Thresh": {
        "id": 412,
        "key": "Thresh",
        "name": "Thresh",
        "title": "the Chain Warden",
        "image": {
            "full": "Thresh.png",
            "sprite": "champion3.png",
            "group": "champion",
            "x": 336,
            "y": 0,
            "w": 48,
            "h": 48
        },

 "Aatrox": {
        "id": 266,
        "key": "Aatrox",
        "name": "Aatrox",
        "title": "the Darkin Blade",
        "image": {
            "full": "Aatrox.png",
            "sprite": "champion0.png",
            "group": "champion",
            "x": 0,
            "y": 0,
            "w": 48,
            "h": 48
        },

如果你想通過每個冠軍,我建議在PHP中使用foreach循環。 您可以這樣使用它:

foreach($json->data as $champion)
{
    // Do something
}

暫無
暫無

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

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