如何在PHP中访问.json数据?

时间:2016-02-22 07:44:11

标签: php json

我有一个.json文件。我想在我的.php文件中访问这些数据。我有一个变量$ spellId,例如:$ spellId =" SummonerBarrier&#34 ;;

我想得到"姓名"来自.json文件只需要$ spellId。 知道我怎么能这样做吗?

我知道这里有很多这样的问题,但我不知道如何用我的代码制作解决方案。

{
    "type": "summoner",
    "version": "6.3.1",
    "data": {
        "SummonerBarrier": {
            "id": "SummonerBarrier",
            "name": "Barrier",
            "description": "Shields your champion for 115-455 (depending on champion level) for 2 seconds.",
            "tooltip": "Temporarily shields {{ f1 }} damage from your champion for 2 seconds.",
            "maxrank": 1,
            "cooldown": [
                210
            ]
        },
        "SummonerBoost": {
            "id": "SummonerBoost",
            "name": "Cleanse",
            "description": "Remove..."
        }
    }
}

3 个答案:

答案 0 :(得分:0)

试试这个:

$data = '{
    "type": "summoner",
    "version": "6.3.1",
    "data": {
        "SummonerBarrier": {
            "id": "SummonerBarrier",
            "name": "Barrier",
            "description": "Shields your champion for 115-455 (depending on champion level) for 2 seconds.",
            "tooltip": "Temporarily shields {{ f1 }} damage from your champion for 2 seconds.",
            "maxrank": 1,
            "cooldown": [
                210
            ]
        },
        "SummonerBoost": {
            "id": "SummonerBoost",
            "name": "Cleanse",
            "description": "Remove..."
        }
    }
}';

$decoded = json_decode($data);
var_dump($decoded->data->SummonerBarrier->name);

答案 1 :(得分:0)

$array = json_decode($json, true);

foreach ($array['data'] as $key => $value) {
    echo "$key:$value[name]<br />";
}

答案 2 :(得分:0)

$decodeData = json_decode($data);
$spellId="SummonerBarrier";
$nameToFetch=$decodeData->data->$spellId->name;