1

我试图在 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"}
4

1 回答 1

2

如果没有看到您的数据,很难判断,但可能的原因是索引不是连续的(0, 2, 5, 6而不是0, 1, 2, 3)。

如果您不确定数据包含什么,最好使用foreach循环遍历您的数组:

foreach ($json_data as $key => $value) 
{
  ...
}
于 2013-02-25T21:57:47.127 回答