JSON PHP:这是正确的方法吗?
我只是想要一些关于我使用 JSON 的信息。
<?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会更好吗?
有关于这个主题的任何指南吗(希望不是整本书)?
谢谢。
I just wanted some input about my use of JSON.
<?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);
?>
This is just some test code, not an actual implementation. Am I using JSON correctly and too the fullest? Or is some better method of doing this?
I have legal torrents that I'd like to do some JSON with.
Torrents are grouped by name which contain multiple torrents (actual links to data). And other information such as codec etc.
This is my first time actually outputting JSON, would XML be better?
Are there any guides on this topic (hopefully not entire books)?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你做的事是对的。我喜欢使用 StdClass 来创建对象而不是键值数组,只是因为它看起来更性感!例如,
正如您所说,您不需要阅读有关此事的整本书,我会在这里看看 http://php.net/manual/en/book.json.php 来掌握 JSON 的基础知识。
就 JSON 与 XML 而言,我发现将数据表示为 JSON 要容易得多,因为更容易获取所需的特定数据,就像访问 stdClass 对象中的信息一样。
[编辑]
正如 Stefan Gehrig 所说,确保将内容类型定义为“application/json”。
What you doing is right. I like to use the StdClass to make objects rather than key value arrays, just cause it looks sexier! E.g.
As you say you don't need to read a whole book on the matter, I would have a look here http://php.net/manual/en/book.json.php to get to grips with the basics of JSON.
In terms of JSON vs XML, I find it far easier to represent data as JSON as it is easier to fetch the specific data you want much in the same way you can access the info in a stdClass object.
[EDIT]
And as Stefan Gehrig says make sure you define your content type as "application/json".
绝对没问题。您只能将 MIME 类型更改为 RFC 4627 兼容:
application/ json
.Absolutely fine. You could only change the MIME type to be RFC 4627 compliant though:
application/json
.