将json解析为PHP变量

时间:2015-06-18 00:57:03

标签: php json

我试图将这个json数据解析成php变量。

[{"type":"browsers","list":[{"key":"mobilesafari","value":3},{"key":"safari","value":1},{"key":"chrome","value":1}],"cardinality":3},{"type":"countries","list":[{"key":"US","value":3},{"key":"KR","value":1},{"key":"BR","value":1}],"cardinality":3},{"type":"languages","list":[{"key":"en-us","value":3},{"key":"pt-br","value":1},{"key":"ko-kr","value":1}],"cardinality":3},{"type":"organisations","list":[{"key":"Williams-Sonoma","value":1},{"key":"Verizon Wireless","value":1},{"key":"SK Telecom","value":1},{"key":"Oi Velox","value":1},{"key":"CenturyLink","value":1}],"cardinality":5},{"type":"platforms","list":[{"key":"iphone","value":2},{"key":"win8","value":1},{"key":"mac108","value":1},{"key":"ipad","value":1}],"cardinality":4}] 

我试图设置它,所以我有一个broswers,国家等的变量。

我尝试过使用

$foo = $json->browsers;

但是我猜不会工作,因为那不是var的名字。

有没有非复杂的方法呢?

由于

3 个答案:

答案 0 :(得分:0)

如评论中所述,您需要先使用json_decode()解码json。

$data = json_decode($json);

现在,您的数据。它是一组对象,这意味着你必须循环它:

foreach($data as $obj) {
    if($obj->type == 'browsers') {
        print_r($obj);
    }
}

Example

答案 1 :(得分:0)

使用json_decode()但传入第二个参数true。这样做将返回一个数组而不是一个对象。然后,您可以循环访问数组以获取每组数据。

$json = '[{"type":"browsers","list":[{"key":"mobilesafari","value":3},{"key":"safari","value":1},{"key":"chrome","value":1}],"cardinality":3},{"type":"countries","list":[{"key":"US","value":3},{"key":"KR","value":1},{"key":"BR","value":1}],"cardinality":3},{"type":"languages","list":[{"key":"en-us","value":3},{"key":"pt-br","value":1},{"key":"ko-kr","value":1}],"cardinality":3},{"type":"organisations","list":[{"key":"Williams-Sonoma","value":1},{"key":"Verizon Wireless","value":1},{"key":"SK Telecom","value":1},{"key":"Oi Velox","value":1},{"key":"CenturyLink","value":1}],"cardinality":5},{"type":"platforms","list":[{"key":"iphone","value":2},{"key":"win8","value":1},{"key":"mac108","value":1},{"key":"ipad","value":1}],"cardinality":4}]';

foreach(json_decode($json,true) as $arrEachItem){
    switch($arrEachItem['type']){
        case'countries': $arrCountries = $arrEachItem['list']; break;
        case'browsers': $arrBrowsers = $arrEachItem['list']; break;
        case'languages': $arrLanguages = $arrEachItem['list']; break;
        case'organisations': $arrOrganisations = $arrEachItem['list']; break;
        case'platforms': $arrPlatforms = $arrEachItem['list']; break;
    }
}

//Output each array to the screen
echo "<pre>";
print_r($arrCountries);
print_r($arrBrowsers);
print_r($arrLanguages);
print_r($arrOrganisations);
print_r($arrPlatforms);

答案 2 :(得分:0)

$data='[{"type":"browsers","list":[{"key":"mobilesafari","value":3},{"key":"safari","value":1},{"key":"chrome","value":1}],"cardinality":3},{"type":"countries","list":[{"key":"US","value":3},{"key":"KR","value":1},{"key":"BR","value":1}],"cardinality":3},{"type":"languages","list":[{"key":"en-us","value":3},{"key":"pt-br","value":1},{"key":"ko-kr","value":1}],"cardinality":3},{"type":"organisations","list":[{"key":"Williams-Sonoma","value":1},{"key":"Verizon Wireless","value":1},{"key":"SK Telecom","value":1},{"key":"Oi Velox","value":1},{"key":"CenturyLink","value":1}],"cardinality":5},{"type":"platforms","list":[{"key":"iphone","value":2},{"key":"win8","value":1},{"key":"mac108","value":1},{"key":"ipad","value":1}],"cardinality":4}]';

if( !function_exists('pre') ){/* utility function for formatted output - aids debugging */
    function pre( $data ){
        echo '<pre>',print_r( $data, 1 ),'</pre>';
    }
}

if( !function_exists('createobject') ){
    function createobject( $data=false ){
        try{
            if( !$data )throw new Exception('Bad value supplied for input data');

            /* decode the supplied data */
            $json=json_decode( $data );

            /* re-use the variable but reassign as an object */
            $data=new stdClass;

            /* Iterate through the json data and construct out output object */
            foreach( $json as $obj ){
                try{
                    /*
                        A temporary array to store each list item
                        ~ could potentially use `array_map` here
                    */
                    $tmp=new stdClass;
                    foreach( $obj->list as $i => $item ){
                        $tmp->{$item->key}=$item->value;
                    }

                    /* Assign new property to output object */
                    $data->{$obj->type}=$tmp;
                }catch( Exception $e ){
                    continue;
                }
            }
            return $data;
        }catch( Exception $e ){
            echo $e->getMessage();
        }
    }
}
/* To use new object */
$obj=createobject( $data );
pre( $obj );
pre( $obj->browsers );
pre( $obj->organisations );
echo $obj->languages->{'en-us'}; //3

这将输出:

stdClass Object
(
    [browsers] => stdClass Object
        (
            [mobilesafari] => 3
            [safari] => 1
            [chrome] => 1
        )

    [countries] => stdClass Object
        (
            [US] => 3
            [KR] => 1
            [BR] => 1
        )

    [languages] => stdClass Object
        (
            [en-us] => 3
            [pt-br] => 1
            [ko-kr] => 1
        )

    [organisations] => stdClass Object
        (
            [Williams-Sonoma] => 1
            [Verizon Wireless] => 1
            [SK Telecom] => 1
            [Oi Velox] => 1
            [CenturyLink] => 1
        )

    [platforms] => stdClass Object
        (
            [iphone] => 2
            [win8] => 1
            [mac108] => 1
            [ipad] => 1
        )

)
stdClass Object
(
    [mobilesafari] => 3
    [safari] => 1
    [chrome] => 1
)
stdClass Object
(
    [Williams-Sonoma] => 1
    [Verizon Wireless] => 1
    [SK Telecom] => 1
    [Oi Velox] => 1
    [CenturyLink] => 1
)
3