2016-01-13 47 views
-1

对PHP不熟悉,并试图弄清楚如何解析API数据,该数据以看起来像奇怪的格式返回。下面是数据的一个样本:用PHP解析JSON - 不规则格式

[{"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"]}] 
+3

你问过之前有没有尝试过任何东西?请谷歌。这不是一个奇怪的格式。这是JSON。 – Kenneth

+2

'json_decode()' – wogsland

+0

究竟是什么不规则?你的标题包括'parse','JSON'和'php',所以我认为你已经尝试过'json_decode'并且有问题 - 这些问题到底是什么? – Steve

回答

1

这里是你如何解析您的JSON作为数组的example

$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]; 

提供了以下结果:

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 

第一我们输出整个数组。根据数据,我们能够为video2.stack.com中的某个阵列部件选择一个值。遍历相对容易,你应该能够提取你需要的任何信息。你甚至可以为你的JSON构建一个递归搜索函数。


注意:我删除了一些数据(部分,...),因为它使你的JSON非有效。