2

I am trying to convert an rss feed to JSON Object, I had some success in converting to JSON object but structure is little off.

Below is the output what I am getting.

{
  "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"
    }
  ]
}

Output what i want

{
  "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"
    }
  ]
}

code which i am using

<?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);

?>

Let me know your thoughts, Thanks in Advance!

1 Answer 1

3

Test with this

try this way for the item iteration

foreach($items as $item) {

  $json['item'][] = array("title"=>$title,"price"=>$price,"description"=>$description)
}
echo json_encode($json)

The reason you got that is because you had given $i++for every array index if you had just given $json['item'][$i] and $i=$i+1 at the end you would have got desired output

So also this will work

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

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

Third approach

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

Apply $i++ only to the last element and $i to the rest becuase $i++ is post increment

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.