访问变量到json代码

时间:2018-05-13 14:59:21

标签: php json

我有这段代码:

Content-Type: application/json
{
    "status": 200,
    "msg": "OK",
    "result": {
       "72fA-_Lq8Ak3": {
            "id": "72fA-_Lq8Ak3",
            "status": 200,
            "name": "The quick brown fox.txt",
            "size": 123456789012,
            "sha1": "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
            "content_type": "plain/text",
        },
        "72fA-_Lq8Ak4": {
            "id": "72fA-_Lq8Ak4",
            "status": 500,
            "name": "The quick brown fox.txt",
            "size": false,
            "sha1": "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
            "content_type": "plain/text",
        },
        "72fA-_Lq8Ak5": {
            "id": "72fA-_Lq8Ak5",
            "status": 404,
            "name": false,
            "size": false,
            "sha1": false,
            "content_type": false,
        },
        "72fA-_Lq8Ak6": {
            "id": "72fA-_Lq8Ak6",
            "status": 451,
            "name": "The quick brown fox.txt",
            "size": 123456789012,
            "sha1": "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
            "content_type": "plain/text",
        }
    }
}

如何在结果中访问php-> 72fA-_Lq8Ak3->状态?

我使用过这段代码:

$control=json_decode($output); $stat=$control->{'result'}->{"".$ll}->{'status'};

但它没有回报我(空)。我如何处理状态代码? 在变量$ll中,代码72fA-_Lq8Ak3但它没有读取变量的值。真正的问题是:我如何将变量插入程序可以读取的字符串?

感谢回复。 抱歉我的英语,但我是法国人。

更新:

我已经有了这个输出,服务器返回给我的输出(在更改之后)是这样的:

{
    "status":200,
    "msg":"OK",
    "result":{
            "TgaB4CzkhaM":{ "id":"TgaB4CzkhaM",
                            "status":200,
                            "name":"esempio2.avi.mp4",
                            "size":"10391713",
                            "sha1":"125d4aa4c039cdf5686d565e705e38cbab966550",
                            "content_type":"video\/mp4",
                            "cstatus":"0"
                        }
            }
} 

我怎样才能进入

results-> TgaB4CzkhaM -> status?

谢谢

1 个答案:

答案 0 :(得分:0)

新答案,新更新

  

我如何输入结果 - > TgaB4CzkhaM - >状态?

<?php
$input='{
    "status":200,
    "msg":"OK",
    "result":{
            "TgaB4CzkhaM":{ "id":"TgaB4CzkhaM",
                            "status":200,
                            "name":"esempio2.avi.mp4",
                            "size":"10391713",
                            "sha1":"125d4aa4c039cdf5686d565e705e38cbab966550",
                            "content_type":"video\/mp4",
                            "cstatus":"0"
                        }
            }
}';
$json=json_decode($input,true);
print_r($json);//this line for print json as array and optional , you can remove this line
if(isset($json["result"]["TgaB4CzkhaM"]["status"]))
{
    echo($json["result"]["TgaB4CzkhaM"]["status"]);
}
else
{
    echo("Error!");
}

输出

Array
(
    [status] => 200
    [msg] => OK
    [result] => Array
        (
            [TgaB4CzkhaM] => Array
                (
                    [id] => TgaB4CzkhaM
                    [status] => 200
                    [name] => esempio2.avi.mp4
                    [size] => 10391713
                    [sha1] => 125d4aa4c039cdf5686d565e705e38cbab966550
                    [content_type] => video/mp4
                    [cstatus] => 0
                )

        )

)
200