从JSON,PHP读取

时间:2016-05-30 14:10:25

标签: php json

来自JSON的示例:

{
    "success": true,
    "num_items": 6698,
    "items": [
        {
            "market_name": "★ Bayonet",
            "market_hash_name": "★ Bayonet",
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQh5hlcX0nvUOGsx8DdQBJjIAVHubSaKQZ53P3NZXMXvYmykdLSxqWkZ7-HkjMIvpIj3u2Y84733gzh_RU_MG_zIYLEdQ45fxiOrdJh0ExF",
            "name_color": "8650AC",
            "quality_color": "EB4B4B"
        },
        {
            "market_name": "★ Bayonet | Blue Steel (Battle-Scarred)",
            "market_hash_name": "★ Bayonet | Blue Steel (Battle-Scarred)",
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-DkvbiKvXTkzNVucNzj7mX9tWk21Xkr0JvN231JYGcdA47NF3Y81Hoxebs1sftot2XnmcyW1u0",
            "name_color": "8650AC",
            "quality_color": "EB4B4B"
        },
        {
            "market_name": "★ Bayonet | Blue Steel (Factory New)",
            "market_hash_name": "★ Bayonet | Blue Steel (Factory New)",
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-HnvD8J_WFxjIBvZIi3OqSo4j3jFbtr0NkY270cNLEJ1RsYV6DqAO8wum5gJ6_ot2Xnpnfakz6",
            "name_color": "8650AC",
            "quality_color": "EB4B4B"
        },
        {
            "market_name": "★ Bayonet | Blue Steel (Field-Tested)",
            "market_hash_name": "★ Bayonet | Blue Steel (Field-Tested)",
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-GkvP9JrafwzlUusN33bDAoI-ijg3sqRJra2DydoaXJAU5YgzUqALrxufmjJe5tIOJlyUq4M_FZA",
            "name_color": "8650AC",
            "quality_color": "EB4B4B"
        },
        {
            "market_name": "★ Bayonet | Blue Steel (Minimal Wear)",
            "market_hash_name": "★ Bayonet | Blue Steel (Minimal Wear)",
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-HnvD8J_WFxjIBvZIi3OqSo4j3jFbtr0NkY270cNLEJ1RsYV6DqAO8wum5gJ6_ot2Xnpnfakz6",
            "name_color": "8650AC",
            "quality_color": "EB4B4B"
        },
        {
            "market_name": "★ Bayonet | Blue Steel (Well-Worn)",
            "market_hash_name": "★ Bayonet | Blue Steel (Well-Worn)",
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-GkvP9JrafwzlUusN33bDAoI-ijg3sqRJra2DydoaXJAU5YgzUqALrxufmjJe5tIOJlyUq4M_FZA",
            "name_color": "8650AC",
            "quality_color": "EB4B4B"
        }
    ]
}

我的PHP代码。我是如何找到icon_url:

function getSkinUrl($json, $skinName)
{
    foreach($json as $key => $value)
    {
        if ($key == "items")
        {
            foreach($value as $key => $value1)
            {
                foreach($value1 as $key => $value)
                {
                    if ($key == "market_name") // && $value == $skinName
                    {
                        if ($value == $skinName)
                        {

                            foreach($value1 as $key => $value)
                            {
                                if ($key == "icon_url")
                                {
                                    $str2 = substr($value, 2);
                                    return "http://" . $str2;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

它没有找到正确的价值。 例如:我搜索★M9刺刀|屠杀(久经沙场) 它找到了刺刀|屠宰(久经沙场)

我如何调用此功能?

$jsondata = file_get_contents($path."/730.json");
ini_set('memory_limit', '-1');
$json = json_decode($jsondata, true);
getSkinUrl($json,"skin name");

由于

2 个答案:

答案 0 :(得分:0)

您应该查看json_decode

$jsonObject = json_decode($json);

foreach ( $jsonObject->items as $item )
    if ( $item->market_name == ...
etc.

答案 1 :(得分:0)

试试json_decode。你只需要一个foreach循环。

$array = json_decode($jsondata, true);
foreach ($array['items'] as $item) {
    if ($item['market_name'] == ...) {
        return $item['icon_url'];
    }
}