如何在php中将RSS提要转换为JSON

如何在php中将RSS提要转换为JSON,php,json,rss,Php,Json,Rss,我正在尝试将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": [ {

我正在尝试将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);

?>

让我知道你的想法,提前谢谢

用这个测试

对项目迭代尝试这种方法

foreach($items as $item) {

  $json['item'][] = array("title"=>$title,"price"=>$price,"description"=>$description)
}
echo json_encode($json)
之所以这样做,是因为如果您刚刚给出了
$json['item'][$i]
$i=$i+1
的话,那么您就为每个数组索引都提供了
$i++
,最后您将获得所需的输出

所以这也会起作用

$i=0;
 foreach($items as $item) {

  $json['item'][$i] = array("title"=>$title,"price"=>$price,"description"=>$description)
  $i=$i+1; 
 }
echo json_encode($json)
第三种方法

$json['item'][$i++]['guid'] = $guid;

仅将
$i++
应用于最后一个元素,将
$i
应用于其余元素,因为
$i++
是增量后的很高兴它对您有所帮助:)