如何从PHP中的JSON准备表格式

时间:2016-02-29 09:36:08

标签: php json

我有JSON字符串,它有n个项目,我想把它转换成表格

Json String

{"HotelSearchResult":{"ResponseStatus":1,"Error":{"ErrorCode":0,"ErrorMessage":""},"TraceId":"70e718ad-a9c2-4e82-9125-e3ee2f1573f1","CityId":"15254","CheckInDate":"2016-03-22","CheckOutDate":"2016-03-24","PreferredCurrency":"INR","NoOfRooms":1,"RoomGuests":[{"NoOfAdults":1,"NoOfChild":0,"ChildAge":[]}],"HotelResults":[{"ResultIndex":1,"HotelCode":"348043","HotelName":"Base Taupo - Hostel \/ Backpacker","HotelCategory":"","StarRating":2,"HotelDescription":"Property Location A stay at Base Taupo - Hostel \/ Backpacker places you in the heart of Taupo, walking distance from Taupo Museum and Art Gallery and Barbary.  This hostel is within Near Taupo Museum and Art Gallery  ","HotelPromotion":"","HotelPolicy":"","Price":{"CurrencyCode":"INR","RoomPrice":1837.47,"Tax":0.00,"ExtraGuestCharge":0,"ChildCharge":0,"OtherCharges":0,"Discount":0.00,"PublishedPrice":1837.47,"PublishedPriceRoundedOff":1837,"OfferedPrice":1837.47,"OfferedPriceRoundedOff":1837,"AgentCommission":0.00,"AgentMarkUp":0.00,"ServiceTax":0,"TDS":0},"HotelPicture":"http:\/\/www.travelboutiqueonline.com\/imageresource.aspx?img=ckZZ2jR\/KJFL8uCNV\/6rUdikLJoSoWwsQ4+ucla4wpAL9fwTuqkLXc3dJ9jA0ZdlXkihFvi968p+KSfi8oYVU8Tj278FBz\/1XkfoRZtaubj\/jenFXq1xBUpWskdqQlXdlSq2E+AsRgAHCH7MUGE+VOR1GoL89wEcl7bKkOpOKoV8u5\/s6kQwUbyMvkUTHXfr\/OjXIi4RNZfESY3+adSUVfiu7NVoTmH5TlFdNsylMzU=","HotelAddress":"7 Tuwharetoa Street, Taupo, , 3330, , , ","HotelContactNo":"","HotelMap":null,"Latitude":"","Longitude":"","HotelLocation":null,"SupplierPrice":null,"RoomDetails":[]},{"ResultIndex":2,"HotelCode":"444277","HotelName":"Camellia Court Family Motel","HotelCategory":"","StarRating":3,"HotelDescription":"Property Location Located in Taupo, Camellia Court Family Motel is convenient to Taupo Bungy and Taupo Museum and Art Gallery.  This motel is within close proximity of Barbary and Near Taupo Bungy  ","HotelPromotion":"","HotelPolicy":"","Price":{"CurrencyCode":"INR","RoomPrice":2721.38,"Tax":0.00,"ExtraGuestCharge":0,"ChildCharge":0,"OtherCharges":0,"Discount":0.00,"PublishedPrice":2721.38,"PublishedPriceRoundedOff":2721,"OfferedPrice":2721.38,"OfferedPriceRoundedOff":2721,"AgentCommission":0.00,"AgentMarkUp":0.00,"ServiceTax":0,"TDS":0},"HotelPicture":"http:\/\/www.travelboutiqueonline.com\/imageresource.aspx?img=ckZZ2jR\/KJFL8uCNV\/6rUdikLJoSoWwsQ4+ucla4wpAL9fwTuqkLXc3dJ9jA0ZdlXkihFvi968p+KSfi8oYVU8Tj278FBz\/1XkfoRZtaubj\/jenFXq1xBQF2KZhLrrzlriBhuyokbu67Iabfrn\/oYXyyOQZ\/9ufWgRFwVHZjjFr3Cndi8RUX2MpUZp2C+OI\/3j7hNbZEVxU3aohoWZK3Xd79WBVqb\/DrXZotWZd4wAU=","HotelAddress":"50 Tonga Street, Taupo, , 2730, , , ","HotelContactNo":"","HotelMap":null,"Latitude":"","Longitude":"","HotelLocation":null,"SupplierPrice":null,"RoomDetails":[]},}};

完整数据必须看到它
https://drive.google.com/open?id=0B9VV_J4sKTataU1KdUtReFJyaUk

所需的HTML输出: enter image description here

5 个答案:

答案 0 :(得分:1)

  

你应该尝试以下是我的代码

<?php
    $json='{"HotelSearchResult":value......};
    echo "<table border='1'>";
    echo '<thead><tr><td>Hotel Name </td><td>Price</td><td>Action</td></tr></thead>';
    echo "<tbody>";
    $str = json_decode($json, true);
    foreach ($str['HotelSearchResult']['HotelResults'] as $key=> $Result) {
        echo "<tr><td>".$Result['HotelName']."</td>
            <td>".$Result['Price']['RoomPrice']."</td>
            <td><button type='button' value=".$Result['ResultIndex']." >Select</button></td>
        </tr>";
    }
    echo"</tbody>";
    echo "</table>";
  

以下是o / p

Output screen

答案 1 :(得分:0)

使用json_decode获取PHP数组。将代码粘贴到您的localhost php文件下面(用您的json字符串替换),然后在浏览器中查看结果。

&#13;
&#13;
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<?php
    $myarray = json_decode('{"a":1,"b":2,"c":3,"d":4,"e":5}');//Replace with your json string
    echo "<pre>";
    var_dump($myarray);
    echo "</pre>";
?>
</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用json_decode

mixed json_decode ( string $json 
           [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

然后你将有一个数组(尽可能使用关联索引),你可以用一个简单的foreach循环遍历数据;

$jsonString = '{"HotelSear.....';

$data = json_decode($jsonString);

foreach($data as $k=>$v) {
    echo "key [$k] => value [$v]\n";
}

答案 3 :(得分:0)

您应该使用json_decode来解码json数据,而不仅仅是遍历结构。

$json = '{"HotelSearchResult...';    

echo "<table>";
$data = json_decode($json, true);
foreach ($data['HotelSearchResult']['HotelResults'] as $hotelResult) {
    echo "<tr>
        <td>{$hotelResult['HotelName']}</td>
        <td>{$hotelResult['Price']['RoomPrice']}</td>
    </tr>";
}
echo "</table>";

答案 4 :(得分:-1)

这是你正在寻找

<div cjc-box #box><div cjc-input [(hasfocus)]="box.focus"></div></div>