0

I have a HotUKDeals API which when using the URL bellow gives me some JSON. Im trying to use PHP to display it in a table but if i try and use a loop to go through the JSON i get errors.

If i try and decode i get "Object of class stdClass could not be converted to string"

So far I have this which i no gets the json and stores it in the variable .

I also have some JS which will go through JSON and put it in a table , but im having difficulties getting the JSON in the php variable into the JS.

Needed to remove code for work reasons !
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
JohnyChew
  • 147
  • 1
  • 2
  • 10

1 Answers1

0

To get the JSON into your jQuery code you'll have to use AJAX:

PHP file -

<?php 
    $tester = file_get_contents('http://api.hotukdeals.com/rest_api/v2/?key=d2c088ea340558b3b3517396963a341c&results_per_page=2&output=json');
    echo $tester;
?>

jQuery AJAX -

$.get( "hotukdeals.php", function( data ) {
    // now you can work with `data`
    var JSON = jQuery.parseJSON( data ); // it will be an object
    $.each(JSON.deals.items, function( index, value ) {
       console.log( value.title + ' ' + value.description );
    });
});

NOTE: The JSON that is returned has complex construction, so you will have to take care about how you extract during the loop and then place in the table.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119