简体   繁体   English

需要帮助显示 JSON 数据

[英]Need help to display JSON data

I am requesting a JSON data feed and getting a successful return of data.我正在请求 JSON 数据馈送并成功返回数据。 I am trying to create the PHP code to display the results on my website and I'm a bit stuck on part of it.我正在尝试创建 PHP 代码以在我的网站上显示结果,但我有点卡在其中的一部分上。 I have spent days trying to figure it out and have done numerous searches.我花了几天时间试图弄清楚并进行了多次搜索。

I have shown the JSON data returned below and have split it into three parts to show where my problem is.我已经展示了下面返回的 JSON 数据,并将其分成三个部分以显示我的问题所在。 The central part within the square brackets I can show OK with the code I have worked out, also shown further down.方括号内的中心部分我可以用我制定的代码显示 OK,也显示在下面。 But the first part and the very final part I just cannot work out.但是第一部分和最后一部分我无法解决。

{"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"}

After lots of reading this is how far I have got with the code as I say the main part between the square brackets displays but I can't sort the top or bottom parts to display.经过大量阅读,这是我对代码的了解程度,因为我说方括号之间的主要部分显示,但我无法对要显示的顶部或底部部分进行排序。

<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>

Your JSON structure is an array of items, but the issue you have is that some of your items are then further arrays.您的 JSON 结构是一个项目数组,但您遇到的问题是您的一些项目是进一步的 arrays。 This is the nature of JSON:)这就是 JSON 的性质 :)

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

This means you're splitting your whole data array into items, which have a key and a value.这意味着您将整个数据数组拆分为具有键和值的项。 ( The variable '$data' for the key could be a bit confusing, by convention we usually use $key.) (键的变量 '$data' 可能有点混乱,按照惯例,我们通常使用 $key。)

What I think you're wanting to accomplish is that you output the field name for each item, and then it's value;我认为您想要完成的是您 output 每个项目的字段名称,然后是它的值;

So inside that foreach() you would write:所以在那个 foreach() 里面你会写:

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

You'll note the usual convention of not having a space between $value and [.您会注意到 $value 和 [ 之间没有空格的通常约定。

Your complexity comes that sometimes that $value is itself an array, so you want to then break that down into its sub elements too:您的复杂性在于有时 $value 本身就是一个数组,因此您也想将其分解为其子元素:

You could do that with a simple test, using is_array, and then act differently if it's an array to if it's a single value.你可以通过一个简单的测试来做到这一点,使用 is_array,然后在它是一个数组时采取不同的行动,如果它是一个单一的值。 Here's a bit of a rewrite with a nested foreach, which assumes that your data only goes two levels deep.这是一个嵌套 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>';
    }
}

You can take the opportunity to filter out items you don't want, by testing for the $key contents, with something like:您可以借此机会通过测试 $key 内容来过滤掉您不想要的项目,例如:

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

If I'm reading this correctly, it appears that the JSON you're getting back from your property search API looks like this:如果我没看错,那么您从属性搜索 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"
}

And that in your above example you've cropped out the raw_data array containing the actual property results.在上面的示例中,您已经裁剪出包含实际属性结果的raw_data数组。

The complete JSON data appears to have three tiers in it.完整的 JSON 数据似乎包含三层。 First there is the root containing some metadata relating to your search (the search status was successful, you searched for houses in postcode "CC3 3FF", etc).首先是根目录,其中包含一些与您的搜索相关的元数据(搜索状态成功,您搜索了邮政编码为“CC3 3FF”的房屋等)。 Within this there is a property called data , which is an object containing more metadata regarding the results of the search.其中有一个名为data的属性,它是一个 object ,其中包含有关搜索结果的更多元数据。 Then within that there's a property called raw_data which is an array of the actual results.然后在其中有一个名为raw_data的属性,它是实际结果的数组。

If you want to print those results you need to access this raw_data array and iterate over it after your json_decode.如果要打印这些结果,则需要访问此raw_data数组并在 json_decode 之后对其进行迭代。 Take a look at the below example - I've put the values inline in the HTML to try and make it more readable for you, along with an example of how to access the top-level data (in this case the postcode):看看下面的例子 - 我已经将值内联在 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