json_decode returns string type instead of object

后端 未结 3 2073
情歌与酒
情歌与酒 2021-01-02 10:49

I\'m passing a JSON-encoded string to json_decode() and am expecting its output to be an object type, but am getting a string type instead. How can I return an

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-02 11:22

    Instead of writing on the JSON array, try putting it into a PHP array first.

     1,
    'b' => 2,
    'c' => 3,
    'd' => 4,
    'e' => 5
    );
    //Then json_encode()
    $json = json_encode($array);
    echo $json;
    die;
    ?>
    

    In you case, you are using ajax. So when you get a success, you can do this:

    $.ajax({
        url: 'example.com',
        data: {
    
        },
        success: function(data) {
            console.log(data);
        }
    });
    

    Where after data inside console.log() can add the json var like data.a, data.b...

    Also, with the string you providedm you do not need to json_encode since it is alrady in json format

提交回复
热议问题