试图在json_decode中获取非对象的属性

une*_*eer 0 php json

我是JSON的新手,我有一个以数据库形式从数据库中检索到的json对象

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [data] => {"vehicle":[{"year":"2000","make":"Ac","model":"Aceca","acquired_year":"2016","acquired_month":"2","use":"Business","distance_driven_to_work_or_school":"2","distance_driven_manually":"10000"}],"first_name":"ADAS","last_name":"DSADSADA","email":"asddsa@sda.com","phone":"dsasasa","postal_code":"","drivers":[{"name":"ssada","birth_year":"2016","birth_month":"2","birth_day":"2","gender":"female","martial_status":"Single","license_number_provided":"yes","license_number":"asddasdas","license_type":"","training_completed":"","years_been_listed_on_auto_policy_in_north_america":"No Previous Experience","license_suspensions":"","accidents":"Select","convictions":"Select","cancellation_reason":"","cancellation_year":"","cancellation_month":"","cancellation_day":""}],"considering_renters_to_reduce_rate":"yes","install_winter_tires":"no","park_in_private_driveway":"yes","willing_to_install_device":"no","years_insured_with_current_company":"4 Years","how_heard_about_us":"asdaa"}
            [date] => 2017-11-20 18:17:52
            [status] => 0
        )

)
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试使用json_decode将其转换为数组时,我正在尝试获取非对象的属性这里是我的代码

<?php
    echo "<pre>";
    print_r($quotes);  //works fine uptil here
    $data = json_decode($quotes->data,true);//the error line
    echo "<pre>";
    print_r($data);
?>
Run Code Online (Sandbox Code Playgroud)

我尝试了几种方法,但它没有工作我尝试了一些其他的解决方案,以及最终得到错误任何帮助?

Sau*_*nam 7

这是因为$quotes是一个对象数组.试试$quotes[0]->data,例如:

$data = json_decode($quotes[0]->data,true);
// ------------------------^^^
Run Code Online (Sandbox Code Playgroud)

  • @teeyo:不是*另一个*.第一个不是JSON.OP已正确调用`json_decode`来解码问题中唯一的JSON,上面只是指出他/她如何正确访问包含它的属性. (2认同)