我正在尝试将rss源转换为JSON对象,我在转换为JSON对象方面取得了一些成功,但结构却很少。
以下是我得到的输出。
{
"title": "Woot",
"description": "One Day, One Deal",
"link": "http:\/\/www.woot.com\/?utm_source=version1&utm_medium=rss&utm_campaign=api.woot.com",
"item": [
{
"title": "Bounty Hunter Snooper II Metal Detector"
},
{
"price": "$64.99"
},
{
"type": "New"
},
{
"title": "GIV Mobile Phones with One Month Unlimited Service"
},
{
"price": "$249.99"
},
{
"type": "Refurbished"
}
]
}
输出我想要的东西
{
"title": "Woot",
"description": "One Day, One Deal",
"link": "http:\/\/www.woot.com\/?utm_source=version1&utm_medium=rss&utm_campaign=api.woot.com",
"item": [
{
"title": "Bounty Hunter Snooper II Metal Detector",
"price": "$64.99",
"type": "New"
},
{
"title": "GIV Mobile Phones with One Month Unlimited Service",
"price": "$249.99",
"type": "Refurbished"
}
]
}
我正在使用的代码
<?php
header('Content-Type: application/json');
$feed = new DOMDocument();
$feed->load('RSS Feed Url');
$json = array();
$json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');
$json['item'] = array();
$i = 0;
foreach($items as $item) {
$title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$purchaseurl = $item->getElementsByTagName('purchaseurl')->item(0)->firstChild->nodeValue;
$standardimage = $item->getElementsByTagName('standardimage')->item(0)->firstChild->nodeValue;
$shipping = $item->getElementsByTagName('shipping')->item(0)->firstChild->nodeValue;
$price = $item->getElementsByTagName('price')->item(0)->firstChild->nodeValue;
$condition = $item->getElementsByTagName('condition')->item(0)->firstChild->nodeValue;
$guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;
$json['item'][$i++]['title'] = $title;
$json['item'][$i++]['description'] = $description;
$json['item'][$i++]['purchaseurl'] = $purchaseurl;
$json['item'][$i++]['image'] = $standardimage;
$json['item'][$i++]['shipping'] = $shipping;
$json['item'][$i++]['price'] = $price;
$json['item'][$i++]['type'] = $condition;
$json['item'][$i++]['guid'] = $guid;
}
echo json_encode($json);
?>
让我知道你的想法,提前致谢!
答案 0 :(得分:2)
用这个测试
尝试这种方式进行项目迭代
foreach($items as $item) {
$json['item'][] = array("title"=>$title,"price"=>$price,"description"=>$description)
}
echo json_encode($json)
你得到这个的原因是因为你已经为每个数组索引提供了i ++,如果你刚刚给了$ json [&#39; item&#39;] [$ i]和$ i = $ i + 1你会得到所需的输出
所以这也会起作用
$i=0;
foreach($items as $item) {
$json['item'][$i] = array("title"=>$title,"price"=>$price,"description"=>$description)
$i=$i+1;
}
echo json_encode($json)
第三种方法 $ json [&#39; item&#39;] [$ i ++] [&#39; guid&#39;] = $ guid;仅将$ i ++应用于最后一个元素,并将$ i应用于其余元素,因为i ++是后增量< / p>