如何正确解析此JSON?

时间:2019-01-07 17:12:20

标签: php

在尝试解析JSON时,我很难获取值。这是我正在使用的示例。

{
"key": "",
"data":"[{
    \"id\":1,
    \"urls\":[{
        \"url1\":\"http://example1.com\",
        \"url2\":\"http://example2.com\"
    }],
    \"cat_id\":1,
    \"info\":{
        \"color\":\"red\"
    },
    \"description\":\"Example Description\",
    \"extra_info\":{
        \"stuff\":\"example stuff\",
        \"sizes\":[{
            \"size1\":\"small\",
            \"size2\":\"large\"
    }]},
    \"currently_available\":true,
    \"stock\":15
    },
    {
    \"id\":2,
    \"urls\":[{
        \"url1\":\"http://example3.com\",
        \"url2\":\"http://example4.com\"
        }],
        \"cat_id\":2,
        \"info\":{
            \"color\":\"blue\"
        },
        \"description\":\"Example Description\",
        \"extra_info\":{
            \"stuff\":\"example stuff\",
            \"sizes\":[{
                \"size1\":\"small\",
                \"size2\":\"large\"
        }]},
    \"currently_available\":true,
    \"stock\":15}]"
}

我通常无法解析JSON,所以我想到了这个。

$json = '
{
     "key": "",
     "data":"[{\"id\":1,\"urls\":[{\"url1\":\"http://example1.com\",\"url2\":\"http://example2.com\"}],\"cat_id\":1,\"info\":{\"color\":\"red\"},\"description\":\"Example Description\",\"extra_info\":{\"stuff\":\"example stuff\",\"sizes\":[{\"size1\":\"small\",\"size2\":\"large\"}]},\"currently_available\":true,\"stock\":15},{\"id\":2,\"urls\":[{\"url1\":\"http://example3.com\",\"url2\":\"http://example4.com\"}],\"cat_id\":2,\"info\":{\"color\":\"blue\"},\"description\":\"Example Description\",\"extra_info\":{\"stuff\":\"example stuff\",\"sizes\":[{\"size1\":\"small\",\"size2\":\"large\"}]},\"currently_available\":true,\"stock\":15}]"
}
';

$result = json_decode($json);
$arr = array();
$values = Extract_Vals($result->data, $arr);
print_r($values);

function Extract_Vals($data, $arr){
    $pattern = '@(?:"id":)([\d]+)@';
    preg_match_all($pattern, $data, $m);  
    $pattern = '@(?:"url1":")([\w\W]*?)(")@';
    preg_match_all($pattern, $data, $m2); 
    $pattern = '@(?:"url2":")([\w\W]*?)(")@';
    preg_match_all($pattern, $data, $m3); 
    $pattern = '@(?:"cat_id":)([\d]+)@';
    preg_match_all($pattern, $data, $m4); 
    $pattern = '@(?:"color":")([\w\W]*?)(")@';
    preg_match_all($pattern, $data, $m5); 
    $pattern = '@(?:"description":")([\w\W]*?)(")@';
    preg_match_all($pattern, $data, $m6); 
    $pattern = '@(?:"size1":")([\w\W]*?)(")@';
    preg_match_all($pattern, $data, $m7); 
    $i = 0;
    foreach($m[1] as $e){
        $data_array = Array(
        $e,
        $m2[1][$i],
        $m3[1][$i],
        $m4[1][$i],
        $m5[1][$i],
        $m6[1][$i],
        $m7[1][$i]
    );
    $arr[] = $data_array;
    $i++;
}
return $arr;  
}

我的代码似乎可以正常工作,但是通常我只是做一个简单的循环,所以我想知道是否有人有更好的主意来获取值,或者是否可以向我展示如何在常规循环中正确获取它们。谢谢。

1 个答案:

答案 0 :(得分:2)

似乎与data键关联的值也是JSON。使用json_decode($result->data)对其进行解码。

我建议您将TRUE作为第二个参数传递给json_decode(),以要求它产生数组而不是对象。 PHP数组比stdClass实例更具通用性,并且有数十个array functions可以处理它们。

您需要的所有代码是:

$json = '
{
     "key": "",
     "data":"[{\"id\":1,\"urls\":[{\"url1\":\"http://example1.com\",\"url2\":\"http://example2.com\"}],\"cat_id\":1,\"info\":{\"color\":\"red\"},\"description\":\"Example Description\",\"extra_info\":{\"stuff\":\"example stuff\",\"sizes\":[{\"size1\":\"small\",\"size2\":\"large\"}]},\"currently_available\":true,\"stock\":15},{\"id\":2,\"urls\":[{\"url1\":\"http://example3.com\",\"url2\":\"http://example4.com\"}],\"cat_id\":2,\"info\":{\"color\":\"blue\"},\"description\":\"Example Description\",\"extra_info\":{\"stuff\":\"example stuff\",\"sizes\":[{\"size1\":\"small\",\"size2\":\"large\"}]},\"currently_available\":true,\"stock\":15}]"
}
';

$result = json_decode($json, TRUE);
$data = json_decode($result['data'], TRUE);

快速浏览$data可以发现它已经正确编码并且可以解码(如果无法解码,则为NULL

Array
(
    [0] => Array
        (
            [id] => 1
            [urls] => Array
                (
                    [0] => Array
                        (
                            [url1] => http://example1.com
                            [url2] => http://example2.com
                        )
                )
            [cat_id] => 1
            [info] => Array
                (
                    [color] => red
                )
            [description] => Example Description
            [extra_info] => Array
                (
                    [stuff] => example stuff
                    [sizes] => Array
                        (
                            [0] => Array
                                (
                                    [size1] => small
                                    [size2] => large
                                )
                        )
                )
            [currently_available] => 1
            [stock] => 15
        )
    [1] => Array
        (
            [id] => 2
            [urls] => Array
                (
                    [0] => Array
                        (
                            [url1] => http://example3.com
                            [url2] => http://example4.com
                        )
                )
            [cat_id] => 2
            [info] => Array
                (
                    [color] => blue
                )
            [description] => Example Description
            [extra_info] => Array
                (
                    [stuff] => example stuff
                    [sizes] => Array
                        (
                            [0] => Array
                                (
                                    [size1] => small
                                    [size2] => large
                                )
                        )
                )
            [currently_available] => 1
            [stock] => 15
        )
)