简体   繁体   中英

Decode json and display in table

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 !

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM