簡體   English   中英

需要幫助顯示 JSON 數據

[英]Need help to display JSON data

我正在請求 JSON 數據饋送並成功返回數據。 我正在嘗試創建 PHP 代碼以在我的網站上顯示結果,但我有點卡在其中的一部分上。 我花了幾天時間試圖弄清楚並進行了多次搜索。

我已經展示了下面返回的 JSON 數據,並將其分成三個部分以顯示我的問題所在。 方括號內的中心部分我可以用我制定的代碼顯示 OK,也顯示在下面。 但是第一部分和最后一部分我無法解決。

{"status":"success","postcode":"CC3 3FF","postcode_type":"full","url":"https:\/\/propjsondata.co.uk\/draw?input=AA3+3FF","type":"terraced_house","max_age":18,"data":{"points_analysed":50,"radius":"1.12","date_earliest":"2019-01-14","date_latest":"2020-02-14","average":266,"70pc_range":[228,316],"80pc_range":[218,350],"90pc_range":[216,361],"100pc_range":[123,493],"raw_data":

    ///With the first part it doesn’t work how do I code this to display///The following between the square brackets displays fine///

[{"date":"2019-11-14","address":"20, Crowhurst Road, AA3 3JW","price":247000,"bedrooms":3,"type":null,"sqf":678,"price_per_sqf":364,"distance":"0.07"},{"date":"2019-08-16","address":"23, Crowhurst Road, AA3 3JW","price":225000,"bedrooms":3,"type":null,"sqf":1033,"price_per_sqf":218,"distance":"0.07"},{"date":"2019-02-26","address":"27, Crowhurst Road, BB3 3JW","price":246000,"bedrooms":3,"type":null,"sqf":775,"price_per_sqf":317,"distance":"0.07"},{"date":"2019-05-31","address":"39, Papillon Road, CC3 3JG","price":231000,"bedrooms":3,"type":null,"sqf":732,"price_per_sqf":316,"distance":"0.16"},{"date":"2019-09-27","address":"85, Creffield Road, CC3 3JB","price":352000,"bedrooms":3,"type":null,"sqf":1152,"price_per_sqf":306,"distance":"0.35"},{"date":"2019-04-08","address":"67, Creffield Road, DD3 3JB","price":385000,"bedrooms":3,"type":null,"sqf":1087,"price_per_sqf":354,"distance":"0.35"},{"date":"2019-07-05","address":"61, Butt Road, DD3 3DG","price":238000,"bedrooms":3,"type":null,"sqf":840,"price_per_sqf":283,"distance":"0.37"},{"date":"2019-09-13","address":"83, New Kiln Road, DD3 3QL","price":280000,"bedrooms":3,"type":null,"sqf":1033,"price_per_sqf":271,"distance":"0.42"},{"date":"2019-03-22","address":"74, North Station Road, EE1 1SE","price":221000,"bedrooms":3,"type":null,"sqf":1798,"price_per_sqf":123,"distance":"0.45"},{"date":"2019-01-14","address":"6, Garland Road, FF2 7GD","price":272000,"bedrooms":3,"type":null,"sqf":1170,"price_per_sqf":232,"distance":"0.46"},{"date":"2019-09-20","address":"33, Wickham Road, GG3 3ED","price":280000,"bedrooms":3,"type":null,"sqf":775,"price_per_sqf":361,"distance":"0.48"}]

   ///if I add this it stops working how do I code this to display, happy to leave out though///

},"process_time":"2.53"}

經過大量閱讀,這是我對代碼的了解程度,因為我說方括號之間的主要部分顯示,但我無法對要顯示的頂部或底部部分進行排序。

<HTML>
<HEAD>
<STYLE>
table, th, td {
  border: 1px solid black;
}
th, td {
  padding: 10px;
}
</STYLE></HEAD>
<BODY>
<TABLE>

<?php
$url= 'https://81b.co.uk/sold_prices.json';
$json = file_get_contents($url);
$resultdata = json_decode($json, true);
foreach ($resultdata as $data => $value)

    echo '<tr><td>'.$value ["date"] .'</td><td>'.$value ["address"] .'</td><td>'.$value [“price”]  .'</td><td>'.$value [“bedrooms”]  .'</td><td>'.$value [“type”]  .'</td><td>'.$value [“sqf”]  .'</td><td>'.$value [“price_per_sqf”]  .'</td><td>'.$value [“distance”]  .'</td></tr>';
?> 

</TABLE>  
</BODY>
</HTML>

您的 JSON 結構是一個項目數組,但您遇到的問題是您的一些項目是進一步的 arrays。 這就是 JSON 的性質 :)

foreach ($resultdata as $data => $value)

這意味着您將整個數據數組拆分為具有鍵和值的項。 (鍵的變量 '$data' 可能有點混亂,按照慣例,我們通常使用 $key。)

我認為您想要完成的是您 output 每個項目的字段名稱,然后是它的值;

所以在那個 foreach() 里面你會寫:

echo '<td>'. $data . ' : ' . $value .'</td>';

您會注意到 $value 和 [ 之間沒有空格的通常約定。

您的復雜性在於有時 $value 本身就是一個數組,因此您也想將其分解為其子元素:

你可以通過一個簡單的測試來做到這一點,使用 is_array,然后在它是一個數組時采取不同的行動,如果它是一個單一的值。 這是一個嵌套 foreach 的重寫,它假設您的數據只有兩層深。

foreach ($resultdata as $key => $value) {
    if (is_array($value)) {
        // It's an array, iterate over it.
        foreach($value as $subkey => $subvalue) {
           echo '<td>'. $subkey . ' : ' . $subvalue .'</td>';
        }
    } else {
       // it's a single value
       echo '<td>'. $data . ' : ' . $value .'</td>';
    }
}

您可以借此機會通過測試 $key 內容來過濾掉您不想要的項目,例如:

if(!(in_array($key, ['address','postcode']))) {

如果我沒看錯,那么您從屬性搜索 API 中返回的 JSON 看起來像這樣:

{
   "status":"success",
   "postcode":"CC3 3FF",
   "postcode_type":"full",
   "url":"https:\/\/propjsondata.co.uk\/draw?input=AA3+3FF",
   "type":"terraced_house",
   "max_age":18,
   "data":{
      "points_analysed":50,
      "radius":"1.12",
      "date_earliest":"2019-01-14",
      "date_latest":"2020-02-14",
      "average":266,
      "70pc_range":[
         228,
         316
      ],
      "80pc_range":[
         218,
         350
      ],
      "90pc_range":[
         216,
         361
      ],
      "100pc_range":[
         123,
         493
      ],
      "raw_data":[
         {
            "date":"2019-11-14",
            "address":"20, Crowhurst Road, AA3 3JW",
            "price":247000,
            "bedrooms":3,
            "type":null,
            "sqf":678,
            "price_per_sqf":364,
            "distance":"0.07"
         },
         {
            "date":"2019-08-16",
            "address":"23, Crowhurst Road, AA3 3JW",
            "price":225000,
            "bedrooms":3,
            "type":null,
            "sqf":1033,
            "price_per_sqf":218,
            "distance":"0.07"
         },
         {
            "date":"2019-02-26",
            "address":"27, Crowhurst Road, BB3 3JW",
            "price":246000,
            "bedrooms":3,
            "type":null,
            "sqf":775,
            "price_per_sqf":317,
            "distance":"0.07"
         },
         {
            "date":"2019-05-31",
            "address":"39, Papillon Road, CC3 3JG",
            "price":231000,
            "bedrooms":3,
            "type":null,
            "sqf":732,
            "price_per_sqf":316,
            "distance":"0.16"
         },
         {
            "date":"2019-09-27",
            "address":"85, Creffield Road, CC3 3JB",
            "price":352000,
            "bedrooms":3,
            "type":null,
            "sqf":1152,
            "price_per_sqf":306,
            "distance":"0.35"
         },
         {
            "date":"2019-04-08",
            "address":"67, Creffield Road, DD3 3JB",
            "price":385000,
            "bedrooms":3,
            "type":null,
            "sqf":1087,
            "price_per_sqf":354,
            "distance":"0.35"
         },
         {
            "date":"2019-07-05",
            "address":"61, Butt Road, DD3 3DG",
            "price":238000,
            "bedrooms":3,
            "type":null,
            "sqf":840,
            "price_per_sqf":283,
            "distance":"0.37"
         },
         {
            "date":"2019-09-13",
            "address":"83, New Kiln Road, DD3 3QL",
            "price":280000,
            "bedrooms":3,
            "type":null,
            "sqf":1033,
            "price_per_sqf":271,
            "distance":"0.42"
         },
         {
            "date":"2019-03-22",
            "address":"74, North Station Road, EE1 1SE",
            "price":221000,
            "bedrooms":3,
            "type":null,
            "sqf":1798,
            "price_per_sqf":123,
            "distance":"0.45"
         },
         {
            "date":"2019-01-14",
            "address":"6, Garland Road, FF2 7GD",
            "price":272000,
            "bedrooms":3,
            "type":null,
            "sqf":1170,
            "price_per_sqf":232,
            "distance":"0.46"
         },
         {
            "date":"2019-09-20",
            "address":"33, Wickham Road, GG3 3ED",
            "price":280000,
            "bedrooms":3,
            "type":null,
            "sqf":775,
            "price_per_sqf":361,
            "distance":"0.48"
         }
      ]
   },
   "process_time":"2.53"
}

在上面的示例中,您已經裁剪出包含實際屬性結果的raw_data數組。

完整的 JSON 數據似乎包含三層。 首先是根目錄,其中包含一些與您的搜索相關的元數據(搜索狀態成功,您搜索了郵政編碼為“CC3 3FF”的房屋等)。 其中有一個名為data的屬性,它是一個 object ,其中包含有關搜索結果的更多元數據。 然后在其中有一個名為raw_data的屬性,它是實際結果的數組。

如果要打印這些結果,則需要訪問此raw_data數組並在 json_decode 之后對其進行迭代。 看看下面的例子 - 我已經將值內聯在 HTML 中,以嘗試使其更具可讀性,以及如何訪問頂級數據的示例(在本例中為郵政編碼):

<?php
    $url= 'https://81b.co.uk/sold_prices.json';

    $json = file_get_contents($url);
    $resultData = json_decode($json, true);
?>

<html>
    <head>
        <style>
            table, th, td {
              border: 1px solid black;
            }
            th, td {
              padding: 10px;
            }
        </style>
    </head>

    <body>

        <h1>Search for houses near <?php echo $resultData['postcode']; ?></h1>
        <table>
            <?php foreach ($resultData['data']['raw_data'] as $property) { ?>
                <tr>
                    <td><?php echo $property['date']; ?></td>
                    <td><?php echo $property['address']; ?></td>
                    <td><?php echo $property['price']; ?></td>
                    <td><?php echo $property['bedrooms']; ?></td>
                    <td><?php echo $property['type']; ?></td>
                    <td><?php echo $property['sqf']; ?></td>
                    <td><?php echo $property['price_per_sqf']; ?></td>
                    <td><?php echo $property['distance']; ?></td>
                </tr>
            <?php } ?>
        </table>

    </body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM