Php 在这个场景中读取JSON?
我试图解析来自不同来源的rss提要,其中一个来源是:Php 在这个场景中读取JSON?,php,json,yql,Php,Json,Yql,我试图解析来自不同来源的rss提要,其中一个来源是:http://feeds.feedburner.com/DiscoveryNews-Top-Stories 但是这个源代码给了我一些奇怪的json数据,比如: "item": [ { "title": [ "Snazzy Science Photos of the Week (August 10-16)", { "type": "html", "content": "Snaz
http://feeds.feedburner.com/DiscoveryNews-Top-Stories
但是这个源代码给了我一些奇怪的json数据,比如:
"item": [
{
"title": [
"Snazzy Science Photos of the Week (August 10-16)",
{
"type": "html",
"content": "Snazzy Science Photos of the Week (August 10-16)"
}
],
"description": [
"Glowing rabbits, treasure-hunting badgers and a case of mistaken UFO identity help round out this week's photos.<img src=\"http://feeds.feedburner.com/~r/DiscoveryNews-Top-Stories/~4/S6Urfvdw2DQ\" height=\"1\" width=\"1\"/>",
{
"type": "html",
"content": "Glowing rabbits, treasure-hunting badgers and a case of mistaken UFO identity help round out this week's photos."
}
],
<?php
$url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20feed%20where%20url=%22http://feeds.feedburner.com/DiscoveryNews-Top-Stories%22&format=json&diagnostics=true&callback=cbfunc';
$data = substr(file_get_contents($url), 7, -2);
$json = json_decode($data);
foreach ($json->query->results->item as $item)
{
echo "Title: ", $item->title[0], "\nDescription: ", $item->description[0], "\n";
echo "==================================================\n\n";
}
但是当我试图解析Discovery新闻提要时,上面的代码失败了。请帮助
[编辑]:
我使用YQL从源代码获取等效的JSON。这是它将元素打包为数组的
"title": [
"No Battery Required for This Wireless Device",
{
"type": "html",
"content": "No Battery Required for This Wireless Device"
}
],
您可以这样读取第一个元素:
"item": [
{
"title": [
"Snazzy Science Photos of the Week (August 10-16)",
{
"type": "html",
"content": "Snazzy Science Photos of the Week (August 10-16)"
}
],
"description": [
"Glowing rabbits, treasure-hunting badgers and a case of mistaken UFO identity help round out this week's photos.<img src=\"http://feeds.feedburner.com/~r/DiscoveryNews-Top-Stories/~4/S6Urfvdw2DQ\" height=\"1\" width=\"1\"/>",
{
"type": "html",
"content": "Glowing rabbits, treasure-hunting badgers and a case of mistaken UFO identity help round out this week's photos."
}
],
<?php
$url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20feed%20where%20url=%22http://feeds.feedburner.com/DiscoveryNews-Top-Stories%22&format=json&diagnostics=true&callback=cbfunc';
$data = substr(file_get_contents($url), 7, -2);
$json = json_decode($data);
foreach ($json->query->results->item as $item)
{
echo "Title: ", $item->title[0], "\nDescription: ", $item->description[0], "\n";
echo "==================================================\n\n";
}
我没有看到任何JSON,只有XML可用。@RajatSaxena你能展示(JSFIDLE?)你是如何使用YQL/JSON提要的吗?我只是从PHP解析它,我没有使用javascript。另外,我只粘贴了我在问题中使用的PHP代码。谢谢,这有助于解决问题lot@RajatSaxena哈哈:P把我的坏消息贴出来成了一种习惯。顺便问一下,您不想直接读取XML有什么原因吗?不,我似乎喜欢JSON。@RajatSaxena如果您感兴趣的话,我已经在底部发布了一个简单的示例,说明如何使用SimpleXML读取它;)您知道如何在php中确定字符串是文本还是html吗?