php - file_get_contents($url); 输入json
为情所困
为情所困 2017-05-16 12:58:39
0
10
944

使用file_get_contents($url); 返回json 使用json_decode 无法解析,该怎么办 ,curl 方法也用过,都不管用

为情所困
为情所困

reply all(10)
淡淡烟草味

This problem has been solved by myself. The data returned by the third party is ascll, so it needs to be converted into utf-8 format. It has nothing to do with json_decode

大家讲道理

You need to verify whether the format is correct, do not upload the code on BB:

<?php
function treatJsonString($string)
{
    $jsonData = json_decode($string, true);

    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            return $jsonData;
            break;
        case JSON_ERROR_DEPTH:
            print '[Error] - Maximum stack depth exceeded' . PHP_EOL;
            break;
        case JSON_ERROR_STATE_MISMATCH:
            print '[Error] - Underflow or the modes mismatch' . PHP_EOL;
            break;
        case JSON_ERROR_CTRL_CHAR:
            print '[Error] - Unexpected control character found' . PHP_EOL;
            break;
        case JSON_ERROR_SYNTAX:
            print '[Error] - Syntax error, malformed JSON' . PHP_EOL;
            break;
        case JSON_ERROR_UTF8:
            print '[Error] - Malformed UTF-8 characters, possibly incorrectly encoded' . PHP_EOL;
            break;
        default:
            print '[Error] - Unknown error' . PHP_EOL;
            break;
    }
    return null;
}


$jsonString = '{"x":123,"s":[{"a":"1"}]';

var_dump(treatJsonString($jsonString));
滿天的星座

First check whether your json is in normal json format
Then check whether your php file is utf-8 without BOM
I have encountered similar problems before, and it will be fine after removing the BOM~

迷茫

Send the returned data and take a look

伊谢尔伦

No problem, I analyzed it:

世界只因有你
json_decode($json, true)

With true, it means it will be parsed into an array of php

PHPzhong

Confirm first. Is the returned thing json?

为情所困

First, make sure your Json is escaped in other ways. If not, you can use the Json formatting verification tool to check if there is a problem.

Online Json format verification tool
http://www.bejson.com/

習慣沉默

No problem, I suggest you check it carefully

黄舟

Double check whether the JSON data format is correct

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!