Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP解析JSON-不规则格式_Php_Json - Fatal编程技术网

用PHP解析JSON-不规则格式

用PHP解析JSON-不规则格式,php,json,Php,Json,php新手,正在尝试分析API数据,这些数据以奇怪的格式返回。以下是数据示例: [{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],...,"zierfischforum.at":["1","0","0","0.00"]}] 以

php新手,正在尝试分析API数据,这些数据以奇怪的格式返回。以下是数据示例:

[{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],...,"zierfischforum.at":["1","0","0","0.00"]}]
以下是如何将JSON解析为数组的示例:

$json_string = '[{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],"zierfischforum.at":["1","0","0","0.00"]}]';

$json_array = json_decode($json_string, true); // true gets us an array

echo '<pre>';
print_r($json_array);

echo $json_array[1]['video2.stack.com'][0];
首先,我们输出整个数组。根据那里的数据,我们可以为
video2.stack.com
的一个数组部分选择一个值。它相对容易遍历,您应该能够提取所需的任何信息。您甚至可以为JSON构建一个递归搜索函数



注意:我删除了您的一些数据(部分
,…
),因为它使您的JSON无效。

在询问之前,您尝试过什么吗?请用谷歌搜索一下。这不是一种奇怪的格式。它是JSON。什么是不规则的?你的标题包括
parse
JSON
php
,所以我想你已经尝试了
JSON\u解码
,并且遇到了问题-这些问题到底是什么?我使用了JSON\u解码。在没有键值的意义上,这似乎是不规则的。例如,此输出提供有关各个域的信息。在我遇到的所有示例中,您都可以通过“{”domain“{”video2.stack.com”中的类键域获取此信息。您将从json_decode获得的结构将是一个对象的数字索引数组。您必须对其进行迭代才能获得所需的特定对象。
Array
(
    [0] => Array
        (
            [campaign_id] => 9000
            [date] => 2016-01-11
            [totalcount] => 1838
            [page] => 1
            [totalpages] => 1
            [index] => 1
            [count] => 1838
        )

    [1] => Array
        (
            [video2.stack.com] => Array
                (
                    [0] => 84254
                    [1] => 105
                    [2] => 0
                    [3] => 83.71
                )

            [zierfischforum.at] => Array
                (
                    [0] => 1
                    [1] => 0
                    [2] => 0
                    [3] => 0.00
                )

        )

)
84254