我只是想要一些關於我使用JSON的輸入。JSON PHP:這是正確的方法嗎?
<?php
header('Content-Type: text/plain');
//results
$results = array();
for($i=0;$i<20;$i++)
{
$result = array();
$result['name'] = 'Test Season '.ceil(($i+1)/13).' Episode '.(($i%13)+1);
//$result['torrent'] = 'https://www.example.com/?id='.$i.'&key='.uniqid();
$result['torrents'] = array();
$c = mt_rand(1,4);
for($j=0;$j<$c;$j++)
{
$torrent = array();
$torrent['url'] = 'https://www.example.com/?id='.uniqid().'&key='.md5(uniqid());
$torrent['codec'] = $j%2 == 0 ? 'xvid' : 'h264';
$torrent['resolution'] = '720p';
$result['torrents'][] = $torrent;
}
$results[] = $result; //push
}
echo json_encode($results);
?>
這只是一些測試代碼,而不是實際的實現。我是否正確使用JSON並且是最充分的?或者是一個更好的方法來做到這一點?
我有合法我想做一些JSON與山洪。 種子按名稱分組,其中包含多個種子(數據的實際鏈接)。和其他信息,如編解碼器等
這是我第一次真正輸出JSON,XML會更好嗎?
有沒有關於這個主題的任何指南(希望不是整本書)?
謝謝。
你應該使用Content-type:application/json作爲標題順便說一句我不知道其他方式做到這一點... – MiPnamic 2011-04-19 09:26:00
啊,我忘了使用該內容類型。 – Steven 2011-04-19 09:26:29
JSON只是表示相同數據的一種不同方式(正如名稱所示:javascript對象NOTATION)。如果你只是將它用於這些簡單的數組,JSON將會完美無瑕。 – 2011-04-19 09:26:33