【问题标题】:API Data Return to HTMLAPI 数据返回 HTML
【发布时间】:2015-09-29 04:32:42
【问题描述】:

我已将新数据添加到我的 API。我想把它作为纯文本返回

这是 PHP 返回的 API 响应。

 {
    "apiVersion":"1.0", 
    "data":{ 
            "location":"London",:
            { 
             "pressure":"1021",
              "temperature":"23", 
              "skytext":"Sky is Clear",
              "humidity":"40", 
              "wind":"18.36 km/h", 
              "date":"07-10-2015", 
              "day":"Friday" 
             }
      }

我想在我的 html 页面上返回压力值,以便我的用户可以看到读数。我在显示它时遇到问题。

这是我的 PHP api.php

require_once('./includes/config.php');
require_once('./includes/functions.php');
error_reporting(0);
header('Content-Type: text/plain; charset=utf-8;');

$city = $_GET['city'];

if(isset($city)) {

    $weather = new Weather($conf['apikey'], $_GET['f']);
    $weather_current = $weather->get($city, 0, 0, null, null);

    $now = $weather->data(0, $weather_current);

    if($now['location'] !== NULL) {
        echo '{"apiVersion":"1.0", "data":{ "location":"'.$now['location'].'", "temperature":"'.$now['temperature'].'", "pressure":"'.$now['pressure'].'", "skytext":"'.$now['description'].'", "humidity":"'.$now['humidity'].'", "wind":"'.$now['windspeed'].'", "date":"'.$now['date'].'", "day":"'.$now['day'].'" } }';
    } else {
        echo '{"apiVersion":"1.0", "data":{ "error":"The \'city\' requested is not available, make sure it\'s a valid city." } }';
    }
} else {
    echo '{"apiVersion":"1.0", "data":{ "error":"You need to specify the city parameter" } }';
}

【问题讨论】:

  • {"apiVersion":"1.0", "data":{ "location":"London":{ "pressure":"1021", "temperature":"23", "skytext":"Sky is Clear", "humidity":"40", "wind":"18.36 km/h", "date":"07-10-2015", "day":"Friday" } }
  • 我如何显示 json 结果我仍在尝试调试这只是需要一些帮助来解决问题
  • 如果你能提供更多的上下文,帮助你会容易得多。到目前为止,我们几乎没有什么可合作的。
  • 欢迎来到 Stack Overflow!这个问题的信息有点少。你能分享你尝试过的东西,以及你遇到了什么问题吗?您显示的是 JSON(此时无效),而不是 API。

标签: php json api response


【解决方案1】:

为了从 JSON 源获取数据,您应该使用 json_decode() 方法解析数据。然后,您可以使用第二个参数将其解析为数组。如果省略第二个参数,您将得到一个对象数组。

重要提示:您的 JSON 似乎也有语法错误。我在天气信息前添加了weather 键。

$data = '{
    "apiVersion":"1.0",
    "data":{
        "location":"London",
        "weather":{              // Notice the new key!
            "pressure":"1021",
            "temperature":"23",
            "skytext":"Sky is Clear",
            "humidity":"40",
            "wind":"18.36 km/h",
            "date":"07-10-2015",
            "day":"Friday"
        }
    }
}';

$json = json_decode($data, true);

然后您应该能够以关联数组的形式获取压力。

$pressure = $json['data']['weather']['pressure']; // Equals: 1021

希望对你有所帮助,编码愉快!

【讨论】:

    【解决方案2】:

    首先,您需要验证您的 JSON。它缺少一些使您无法解析它的关键内容。使用 JSONLint 验证您的 JSON。

    修改 JSON 使其有效后,我做了以下操作:

    $json = '{"apiVersion":"1.0", "data":{ "location":"London", "data":{ "pressure":"1021", "temperature":"23", "skytext":"Sky is Clear", "humidity":"40", "wind":"18.36 km/h", "date":"07-10-2015", "day":"Friday" }}}';
    
    $obj_style = json_decode($json);
    $array_style = json_decode($json, true);
    
    echo $obj_style->data->data->pressure;
    echo $array_style['data']['data']['pressure'];
    

    使用json_decode(),我能够设置一种以两种方式解析 JSON 的方法,一次作为对象,一次作为数组(添加 true 标志以数组形式返回结果)。

    从那里您所要做的就是深入了解您想要显示的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 2018-08-05
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      相关资源
      最近更新 更多