解析电视剧(包括剧集)的 iTunes JSON
我正在开发一个个人项目来帮助组织我的 iTunes 库,为此我使用 iTunes 搜索 API。
我目前拥有的代码允许我自动解析该系列本身的信息,但我正在尝试更改它,以便它能够识别一个季节中有多少集并解析它们的信息,而无需我编辑 php 为一季 13 集,然后为下一季编辑 27 集。
这是我当前使用的代码;
$response = file_get_contents($itune_url);
$obj = utf8_decode($response);
$results = json_decode($obj);
$artwork = $results->results[0]->artworkUrl100;
$collectionViewUrl = $results->results[0]->collectionViewUrl;
$collectionType = $results->results[0]->collectionType;
$collectionId = $results->results[0]->collectionId;
$collectionName = $results->results[0]->collectionName;
$trackCount = $results->results[0]->trackCount;
$collectionPrice = $results->results[0]->collectionPrice;
$genre = $results->results[0]->primaryGenreName;
$trackNumber1 = $results->results[1]->trackNumber;
$trackName1 = $results->results[1]->trackName;
$shortDescription1 = $results->results[1]->shortDescription;
$longDescription1 = $results->results[1]->longDescription;
无论如何,有没有办法设置我的 PHP,使其识别 $trackCount 并自动生成下一组结果?例如
$trackNumber1 = $results->results[1]->trackNumber;
$trackName1 = $results->results[1]->trackName;
:然后:
$trackNumber2 = $results->results[2]->trackNumber;
$trackName2 = $results->results[2]->trackName;
任何帮助/建议将不胜感激,谢谢!
I'm working on a personal project to help organise my iTunes library, to do this i'm using the iTunes search api.
The code i currently have allows me to parse the information for the Series itself automatically, but i'm trying to change it so it will recognise how many episodes there is in a season and parse the information for them as well, without me having to edit the php for 13 episodes for one season, then 27 for the next.
This is the code i am currently using;
$response = file_get_contents($itune_url);
$obj = utf8_decode($response);
$results = json_decode($obj);
$artwork = $results->results[0]->artworkUrl100;
$collectionViewUrl = $results->results[0]->collectionViewUrl;
$collectionType = $results->results[0]->collectionType;
$collectionId = $results->results[0]->collectionId;
$collectionName = $results->results[0]->collectionName;
$trackCount = $results->results[0]->trackCount;
$collectionPrice = $results->results[0]->collectionPrice;
$genre = $results->results[0]->primaryGenreName;
$trackNumber1 = $results->results[1]->trackNumber;
$trackName1 = $results->results[1]->trackName;
$shortDescription1 = $results->results[1]->shortDescription;
$longDescription1 = $results->results[1]->longDescription;
Is there anyway to set my PHP so it recognises the $trackCount and automatically generates the next set of results? e.g.
$trackNumber1 = $results->results[1]->trackNumber;
$trackName1 = $results->results[1]->trackName;
Then:
$trackNumber2 = $results->results[2]->trackNumber;
$trackName2 = $results->results[2]->trackName;
Any help/advice would be appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以充分利用
foreach
(docs) 来循环 JSON 结果,并且嵌套数组(docs)来保存所有剧集信息。这可以将理解 JSON 结果的阶段缩短为如下所示。结果数组具有以下结构。
这意味着您可以按编号访问各个剧集,例如剧集
Bushwhacked
的$results['Firefly, Season 1'][3]
,也可以循环播放一季:当然,如果您不关心季节(您的所有结果都返回一个节目的单个季节?)或者想要在结果中获得多个节目(您在搜索什么?!),那么可以使用相同的过程已就业,只是标准不同等等(或更少)结果数组结构的深度。
You could make good use of
foreach
(docs) to loop over the JSON results, and nested arrays (docs) to hold all of the episode information. That could cut down your making-sense-of-the-JSON-result phase into something like the following.The results array has the following structure.
This means that you can access individual episodes by number, like
$results['Firefly, Season 1'][3]
for the episodeBushwhacked
or you can loop over a season:Of course, if you don't care about the season (all your results return a single season of a show?) or want to get multiple shows in the results (what are you searching on?!) then the same process can be employed, just with different criteria and more (or less) depth of the results array structure.