json decode show null

时间:2018-02-17 08:52:53

标签: php json

我想创建一个从Android应用中获取数据的API。但是返回$ decoding show null。

 public function login(){

header("Content-type: application/json");

$content = stripslashes(file_get_contents("php://input"));
$json = str_replace('"', '"', $content);

$decoded = json_decode($json, TRUE);

$response_array['content']=$content;
$response_array['decode']=$decoded;
return json_encode($response_array);

if(!is_array($decoded)){
    $response_array['decode']="fail";
    return json_encode($response_array);
}

enter image description here

1 个答案:

答案 0 :(得分:0)

您的代码看起来无序......试试这个:

public function login() {

    header("Content-type: application/json");

    $content = stripslashes(file_get_contents("php://input"));
    $json = str_replace('"', '"', $content);

    $decoded = json_decode($json, TRUE);

    if (!is_array($decoded)) {
        $response_array['decode']="fail";
        return json_encode($response_array);
    }

    $response_array['content'] = $content;
    $response_array['decode']  = $decoded;
    return json_encode($response_array);
}

您最初显示的代码在执行失败json_decode的处理之前返回。