简体   繁体   English

解码json并显示在表中

[英]Decode json and display in table

I have a HotUKDeals API which when using the URL bellow gives me some JSON. 我有一个HotUKDeals API,当使用以下网址时,该API给了我一些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. 我试图使用PHP在表中显示它,但是如果我尝试使用循环来遍历JSON,则会出错。

If i try and decode i get "Object of class stdClass could not be converted to string" 如果我尝试解码,则会收到“类stdClass的对象无法转换为字符串”的信息

So far I have this which i no gets the json and stores it in the variable . 到目前为止,我没有获取json并将其存储在变量中。

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. 我也有一些JS,它将通过JSON并将其放入表中,但是我很难将php变量中的JSON放入JS中。

Needed to remove code for work reasons !

To get the JSON into your jQuery code you'll have to use AJAX: 要将JSON放入jQuery代码中,您必须使用AJAX:

PHP file - PHP文件-

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

jQuery AJAX - 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. 注意:返回的JSON具有复杂的构造,因此您必须注意在循环过程中如何提取然后放置在表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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