Why does PHP always report an error when using json?
$ Res = '{
"Status": "OK", // interface status, see http://www.heweather.com/documents/api
"Basic": {// basic information
"City": "Beijing", // city name
"Cnty": "China", // country
"Id": "CN101010100", // City ID, see http://www.heweather.com/documents/cn-city-list
"Lat": "39.904000", // city dimension
"Lon": "116.391000", // City longitude
"Update": {// update time
"Loc": "", // local time
"Utc": "" // UTC time
}
}
}';
Var_dump (json_decode ($ res); // display the result
// $ Json = '{"foo": 12345 }';
// $ Obj = json_decode ($ json );
// Print $ obj-> {'foo'}; // 12345
?>
The errors are similar to syntax error, unexpected 'status' (T_STRING) in/Applications/MAMP/htdocs/testjson. php on line 5.
Reply to discussion (solution)
What's your code? Remove the comment in json...
What's your code? Remove the comment in json...
It seems that the comment can be removed and displayed. this is probably the case.
Object (stdClass) #1 (2)
{
["Status"] => string (2) "OK"
["Basic"] => object (stdClass) #2 (6)
{
["City"] => string (6) "Beijing"
["Cnty"] => string (6) "China"
["Id"] => string (11) "CN101010100"
["Lat"] => string (9) "39.904000"
["Lon"] => string (10) "116.391000"
["Update"] => object (stdClass) #3 (2)
{
["Loc"] => string (16"
["Utc"] => string (16"
}
}
}
After the layout, for example, how should we extract the "id" value in this array after $ res = json_decode ($ res )?
$ Obj-> basic-> id
Json_decode ($ json, true );
The output is an array.
It doesn't seem to work. in this way, a single data value cannot be extracted.
After I get a json package from a URL, $ res = curl_exec ($ ch); no matter how it is processed, the entire json package will be displayed, instead of a field I want to extract
$ Ch = curl_init ();
$ Url = 'https: // api.heweather.com/x3/weather? Cityid = cn1010000101 & key = 7081f8010abe4638a86e0c4c1cfee30e ';
// Execute the HTTP request
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ Res = json_decode (curl_exec ($ ch ));
Echo $ res ['Now '] ['cond'] ['txt'];
?>
This request cannot obtain the required weather value
Echo $ res [key ($ res)] [0] ['Now '] ['cond'] ['txt']; // multi-cloud
In fact, you can see that now is in the third-dimensional
Just remove the comment.
$ Res = '{"status": "OK", "basic": {"city": "Beijing", "cnty": "China", "id ": "CN101010100", "lat": "39.904000", "lon": "116.391000", "update": {"loc": "", "utc ": "" }}'; var_dump (json_decode ($ res); // display the result
Object (stdClass) [1]
Public 'status' => string 'OK' (length = 2)
Public 'basic '=>
Object (stdClass) [2]
Public 'city' => string 'Beijing' (length = 6)
Public 'cnty '=> string 'China' (length = 6)
Public 'id' => string 'cn1010100' (length = 11)
Public 'lat' => string '39. 904000 '(length = 9)
Public 'Lon '=> string '116. 391000' (length = 10)
Public 'update' =>
Object (stdClass) [3]
Public 'loc '=> string '2017-07-02' (length = 16)
Public 'utc' => string '2017-07-02 '(length = 16)
The obtained id can be written in this way.
$ Res = '{"status": "OK", "basic": {"city": "Beijing", "cnty": "China", "id ": "CN101010100", "lat": "39.904000", "lon": "116.391000", "update": {"loc": "", "utc ": "" }}'; $ data = json_decode ($ res); // display the result echo $ data-> basic-> id;
CN101010100
Echo $ res [key ($ res)] [0] ['Now '] ['cond'] ['txt']; // multi-cloud
In fact, you can see that now is in the third-dimensional
It seems I still can't do it. I tried this just now and couldn't output anything.
Just remove the comment.
$ Res = '{"status": "OK", "basic": {"city": "Beijing", "cnty": "China", "id ": "CN101010100", "lat": "39.904000", "lon": "116.391000", "update": {"loc": "", "utc ": "" }}'; var_dump (json_decode ($ res); // display the result
Object (stdClass) [1]
Public 'status' => string 'OK' (length = 2)
Public 'basic '=>
Object (stdClass) [2]
Public 'city' => string 'Beijing' (length = 6)
Public 'cnty '=> string 'China' (length = 6)
Public 'id' => string 'cn1010100' (length = 11)
Public 'lat' => string '39. 904000 '(length = 9)
Public 'Lon '=> string '116. 391000' (length = 10)
Public 'update' =>
Object (stdClass) [3]
Public 'loc '=> string '2017-07-02' (length = 16)
Public 'utc' => string '2017-07-02 '(length = 16)
The obtained id can be written in this way.
$ Res = '{"status": "OK", "basic": {"city": "Beijing", "cnty": "China", "id ": "CN101010100", "lat": "39.904000", "lon": "116.391000", "update": {"loc": "", "utc ": "" }}'; $ data = json_decode ($ res); // display the result echo $ data-> basic-> id;
CN101010100
Please take a look at the code on the 7th floor and how to extract the txt of the weather
$ Ch = curl_init (); $ url = 'https: // api.heweather.com/x3/weather? Cityid = cn1020.101 & key = Shanghai'; // execute the HTTP request curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); $ res = json_decode (curl_exec ($ ch), true); // json_decode requires the second parameter, this can be parsed into an array echo $ res [key ($ res)] [0] ['Now '] ['cond'] ['txt'], PHP_EOL; // why do you want to write it like this? look at the output of print_r and you will know print_r ($ res );