在json_decode丢失了大约22个字符之后

时间:2013-02-25 21:54:01

标签: php json

我试图在php中解码一个json文件。

function ReturnJson(){
    $json_data =  json_decode(file_get_contents('data.json'), true);
    for ($i = 0, $len = count($json_data); $i< $len; ++$i){

        $json_data[$i]['num'] = (string) ($i +1);
    }
    print_r($json_data);
    return $json_data;   
   }

但是在这里我注意到我错过了原始文件中缺少的大约20个字符。

思想?

好的这里是一个json文档的小片段,它的巨大但开头本身与原来不同

小原始文件:

  {"events":[{"control":null,"submitter":{"deviceId":"1","sessionId":"1"},"variables":[{"scope":"SESSION","value":"iPhone OS","name":"osName"},{"scope":"SESSION","value":"x86_64","name":"hardwareType"},{"scope":"SESSION","value":"6.0","name":"osVersion"},{"scope":"EVENT","value":"51115abd1e0422c533000002","name":"variant"}],"label":null,"category":null,"location":"ADSettingsViewController","timestamp":"2012-11-16T19:23:57Z","action":null,"type":"PAGEVIEW","versionNumber":"1.0","value":0,"submitted":0,"appId":"50fdc396b9efd172b8000065","controlEvent":null},{"control":null,"submitter":{"deviceId":"1","sessionId":"1"},"variables":[{"scope":"SESSION","value":"iPhone OS","name":"osName"},{"scope":"SESSION","value":"x86_64","name":"hardwareType"},{"scope":"SESSION","value":"6.0","name":"osVersion"},{"scope":"EVENT","value":"51115abd1e0422c533000002","name":"variant"}],"label":null,"category":null,"location":"ADProductViewController","timestamp":"2012-11-16T19:23:59Z","action":null,"type":"PAGEVIEW","versionNumber":"1.0","value":0,"submitted":0,"appId":"50fdc396b9efd172b8000065","controlEvent":null} 

出来的是什么:

 {"control":null,"submitter":{"deviceId":"1","sessionId":"1"},"variables":[{"scope":"SESSION","value":"iPhone OS","name":"osName"},{"scope":"SESSION","value":"x86_64","name":"hardwareType"},{"scope":"SESSION","value":"6.0","name":"osVersion"},{"scope":"EVENT","value":"51115abd1e0422c533000002","name":"variant"}],"label":null,"category":null,"location":"ADSettingsViewController","timestamp":"2013-02-25T16:22:57Z","action":null,"type":"PAGEVIEW","versionNumber":"1.0","value":0,"submitted":0,"appId":"50fdc396b9efd172b8000065","controlEvent":null}{"control":null,"submitter":{"deviceId":"1","sessionId":"1"},"variables":[{"scope":"SESSION","value":"iPhone OS","name":"osName"},{"scope":"SESSION","value":"x86_64","name":"hardwareType"},{"scope":"SESSION","value":"6.0","name":"osVersion"},{"scope":"EVENT","value":"51115abd1e0422c533000002","name":"variant"}],"label":null,"category":null,"location":"ADProductViewController","timestamp":"2013-02-25T16:22:57Z","action":null,"type":"PAGEVIEW","versionNumber":"1.0","value":0,"submitted":0,"appId":"50fdc396b9efd172b8000065","controlEvent":null}{"control":"UIRoundedRectButton","submitter":{"deviceId":"1","sessionId":"1"},"variables":[{"scope":"SESSION","value":"iPhone OS","name":"osName"},{"scope":"SESSION","value":"x86_64","name":"hardwareType"},{"scope":"SESSION","value":"6.0","name":"osVersion"},{"scope":"EVENT","value":"51115abd1e0422c533000002","name":"variant"}],"label":"Checkout Now","category":"ADProductViewController","location":null,"timestamp":"2013-02-25T16:22:57Z","action":"checkoutNowTapped:","type":"EVENT","versionNumber":"1.0","value":0,"submitted":0,"appId":"50fdc396b9efd172b8000065","controlEvent":"UIControlEventTouchUpInside"}

1 个答案:

答案 0 :(得分:2)

很难说没有看到您的数据,但可能的原因是指数不是连续的(0, 2, 5, 6而不是0, 1, 2, 3)。

如果您不确定数据包含哪些内容,最好使用foreach循环遍历数组:

foreach ($json_data as $key => $value) 
{
  ...
}