Json解码并在php中显示

时间:2021-11-04 07:42:41

So i've got a list with local weather details, http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl. And I want to display some of that in formation via php on my site, but can't really find out how JSON is something completely new for me.

所以我有一份当地天气详情列表,http://api.openweathermap.org/data/2.5/weather?q = Schimmert,nl。我希望在我的网站上通过php显示其中的一些信息,但无法真正了解JSON对我来说是一个全新的东西。

And the only thing i managed to do right now is this: http://jeroenonline.biz/JSON/index.php. So this is a simple script:

我现在唯一能做的就是:http://jeroenonline.biz/JSON/index.php。所以这是一个简单的脚本:

$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");
 
$decode = json_decode($getData);
 
echo "<pre>";
print_r($decode);

3 个解决方案

#1


6  

using the link

使用链接

http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl

http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl

without the "." gives me a response

没有“。”给了我一个回应

{
    "message": "Error: Not found city",
    "cod": "404"
}

<?php

$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");

$decode = json_decode($getData);

// accessing it through object
echo $decode->message;
echo "<br/>";
echo $decode->cod;

// accessit via array
// set true the second parameter or the json_decode($encoded_data, TRUE)
// to give you array
$decode = json_decode($getData, TRUE);

echo "<br/>";
echo $decode['message'];
echo "<br/>";
echo $decode['cod'];

so when use the link with the "."

所以当使用带有“。”的链接时。

http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl.

http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl。

gives a response of :

给出了回复:

{
    "coord": {
        "lon": 5.83,
        "lat": 50.91
    },
    "sys": {
        "message": 0.0287,
        "country": "Netherlands",
        "sunrise": 1430884846,
        "sunset": 1430939149
    },
    "weather": [
        {
            "id": 800,
            "main": "Clear",
            "description": "Sky is Clear",
            "icon": "01n"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 284.923,
        "temp_min": 284.923,
        "temp_max": 284.923,
        "pressure": 1012.18,
        "sea_level": 1023.56,
        "grnd_level": 1012.18,
        "humidity": 67
    },
    "wind": {
        "speed": 6.06,
        "deg": 219.002
    },
    "clouds": {
        "all": 0
    },
    "dt": 1430875602,
    "id": 0,
    "name": "Nuth",
    "cod": 200
}

to show the the result

显示结果

// sample to access coord
echo $decode->coord->lon;
echo $decode->coord->lat;

// sample to access sys
echo $decode->sys->message;
echo $decode->sys->country;

// sample to access weather
echo $decode->weather[0]->id;
echo $decode->weather[0]->main;
echo $decode->weather[0]->description;

// sample to access main
echo $decode->main->temp;
echo $decode->main->temp_min;


// sample to access wind
echo $decode->wind->speed;

// sample to access clouds
echo $decode->clouds->all;

echo $decode->id;
echo $decode->name;
echo $decode->cod;

#2


2  

this API http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl. get some data like this

这个API http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl。得到一些像这样的数据

{
    "coord": {
        "lon": 5.83,
        "lat": 50.91
    },
    "sys": {
        "message": 0.039,
        "country": "Netherlands",
        "sunrise": 1430884846,
        "sunset": 1430939149
    },
    "weather": [
        {
            "id": 800,
            "main": "Clear",
            "description": "Sky is Clear",
            "icon": "01n"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 284.923,
        "temp_min": 284.923,
        "temp_max": 284.923,
        "pressure": 1012.18,
        "sea_level": 1023.56,
        "grnd_level": 1012.18,
        "humidity": 67
    },
    "wind": {
        "speed": 6.06,
        "deg": 219.002
    },
    "clouds": {
        "all": 0
    },
    "dt": 1430875157,
    "id": 2749752,
    "name": "Nuth",
    "cod": 200
}

try this

尝试这个

<?php
    $getData = file_get_contents("http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl."); // get your json data

    $decode = json_decode($getData); // decode it to be an object
    // echo "<pre>";
    // print_r($decode);

    // you can get the data with
    echo "Country : " . $decode->sys->country; echo '<br>';
    for($i=0;$i<count($decode->weather);$i++){

        echo "ID : " . $decode->weather[0]->id; echo '<br>';
        echo "Weather : " . $decode->weather[0]->main; echo '<br>';
        echo "Description : " . $decode->weather[0]->description; echo '<br>';
        echo "Icon : " . $decode->weather[0]->icon; echo '<br>';

    }

    echo "Temperature : " . $decode->main->temp; echo '<br>';
    echo "Temp Min : " . $decode->main->temp_min; echo '<br>';
    echo "Temp Max : " . $decode->main->temp_max; echo '<br>';
    echo "Preassure : " . $decode->main->pressure; echo '<br>';
    echo "Sea Level : " . $decode->main->sea_level; echo '<br>';
    echo "Ground Level : " . $decode->main->grnd_level; echo '<br>';
    echo "Humidity : " . $decode->main->humidity; echo '<br>';
    echo "Wind Speed : " . $decode->wind->speed; echo '<br>';
    echo "Wind Degrees : " . $decode->wind->deg; echo '<br>';
    echo "Cloud : " . $decode->clouds->all; echo '<br>';

?>

#3


1  

If you want to use the JSON as an array instead, you just have to pass true as the second paramenter for json_decode.

如果要将JSON用作数组,则只需传递true作为json_decode的第二个参数。

$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");
$decode = json_decode($getData, true);
echo "<pre>";
print_r($decode);

#1


6  

using the link

使用链接

http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl

http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl

without the "." gives me a response

没有“。”给了我一个回应

{
    "message": "Error: Not found city",
    "cod": "404"
}

<?php

$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");

$decode = json_decode($getData);

// accessing it through object
echo $decode->message;
echo "<br/>";
echo $decode->cod;

// accessit via array
// set true the second parameter or the json_decode($encoded_data, TRUE)
// to give you array
$decode = json_decode($getData, TRUE);

echo "<br/>";
echo $decode['message'];
echo "<br/>";
echo $decode['cod'];

so when use the link with the "."

所以当使用带有“。”的链接时。

http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl.

http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl。

gives a response of :

给出了回复:

{
    "coord": {
        "lon": 5.83,
        "lat": 50.91
    },
    "sys": {
        "message": 0.0287,
        "country": "Netherlands",
        "sunrise": 1430884846,
        "sunset": 1430939149
    },
    "weather": [
        {
            "id": 800,
            "main": "Clear",
            "description": "Sky is Clear",
            "icon": "01n"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 284.923,
        "temp_min": 284.923,
        "temp_max": 284.923,
        "pressure": 1012.18,
        "sea_level": 1023.56,
        "grnd_level": 1012.18,
        "humidity": 67
    },
    "wind": {
        "speed": 6.06,
        "deg": 219.002
    },
    "clouds": {
        "all": 0
    },
    "dt": 1430875602,
    "id": 0,
    "name": "Nuth",
    "cod": 200
}

to show the the result

显示结果

// sample to access coord
echo $decode->coord->lon;
echo $decode->coord->lat;

// sample to access sys
echo $decode->sys->message;
echo $decode->sys->country;

// sample to access weather
echo $decode->weather[0]->id;
echo $decode->weather[0]->main;
echo $decode->weather[0]->description;

// sample to access main
echo $decode->main->temp;
echo $decode->main->temp_min;


// sample to access wind
echo $decode->wind->speed;

// sample to access clouds
echo $decode->clouds->all;

echo $decode->id;
echo $decode->name;
echo $decode->cod;

#2


2  

this API http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl. get some data like this

这个API http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl。得到一些像这样的数据

{
    "coord": {
        "lon": 5.83,
        "lat": 50.91
    },
    "sys": {
        "message": 0.039,
        "country": "Netherlands",
        "sunrise": 1430884846,
        "sunset": 1430939149
    },
    "weather": [
        {
            "id": 800,
            "main": "Clear",
            "description": "Sky is Clear",
            "icon": "01n"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 284.923,
        "temp_min": 284.923,
        "temp_max": 284.923,
        "pressure": 1012.18,
        "sea_level": 1023.56,
        "grnd_level": 1012.18,
        "humidity": 67
    },
    "wind": {
        "speed": 6.06,
        "deg": 219.002
    },
    "clouds": {
        "all": 0
    },
    "dt": 1430875157,
    "id": 2749752,
    "name": "Nuth",
    "cod": 200
}

try this

尝试这个

<?php
    $getData = file_get_contents("http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl."); // get your json data

    $decode = json_decode($getData); // decode it to be an object
    // echo "<pre>";
    // print_r($decode);

    // you can get the data with
    echo "Country : " . $decode->sys->country; echo '<br>';
    for($i=0;$i<count($decode->weather);$i++){

        echo "ID : " . $decode->weather[0]->id; echo '<br>';
        echo "Weather : " . $decode->weather[0]->main; echo '<br>';
        echo "Description : " . $decode->weather[0]->description; echo '<br>';
        echo "Icon : " . $decode->weather[0]->icon; echo '<br>';

    }

    echo "Temperature : " . $decode->main->temp; echo '<br>';
    echo "Temp Min : " . $decode->main->temp_min; echo '<br>';
    echo "Temp Max : " . $decode->main->temp_max; echo '<br>';
    echo "Preassure : " . $decode->main->pressure; echo '<br>';
    echo "Sea Level : " . $decode->main->sea_level; echo '<br>';
    echo "Ground Level : " . $decode->main->grnd_level; echo '<br>';
    echo "Humidity : " . $decode->main->humidity; echo '<br>';
    echo "Wind Speed : " . $decode->wind->speed; echo '<br>';
    echo "Wind Degrees : " . $decode->wind->deg; echo '<br>';
    echo "Cloud : " . $decode->clouds->all; echo '<br>';

?>

#3


1  

If you want to use the JSON as an array instead, you just have to pass true as the second paramenter for json_decode.

如果要将JSON用作数组,则只需传递true作为json_decode的第二个参数。

$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");
$decode = json_decode($getData, true);
echo "<pre>";
print_r($decode);