PHP读取响应Json

时间:2018-07-24 07:16:22

标签: php

尝试读取此JSON以获取值(例如电子邮件或ID),但出现错误:

  

注意:尝试在第50行的../get_test.php中获取非对象的属性

array(2) { 
    ["message"]=> string(31) "Request successfully processed." 
    ["response"]=> array(1) { 
        [954129]=> array(32) { 
            ["id"]=> string(6) "954129" 
            ["email"]=> string(31) "somemail@gmail.com" 
            ["created_on"]=> string(10) "1522224345" 
            ["type"]=> string(6) "client" 
            ["status"]=> string(3) "New" 
            ["sub_status"]=> string(11) "Unprocessed" 
            ["active"]=> string(1) "1" 
            ["accounts"]=> array(2) { 
                [0]=> array(8) { 
                    ["id"]=> string(7) "2323232" 
                    ["type"]=> string(4) "live" 
                    ["status"]=> string(6) "active" 
                    ["is_default"]=> string(1) "0" 
                    ["leverage"]=> string(2) "10" 
                    ["base_currency"]=> string(3) "USD" 
                    ["created_on"]=> string(10) "1522224478" 
                    ["group"]=> string(9) "TSTF24USD" 
                } 
                [1]=> array(8) { 
                    ["id"]=> string(7) "9131428" 
                    ["type"]=> string(4) "live" 
                    ["status"]=> string(6) "active" 
                    ["is_default"]=> string(1) "1" 
                    ["leverage"]=> string(1) "1" 
                    ["base_currency"]=> string(3) "USD" 
                    ["created_on"]=> string(10) "1522224722310" 
                    ["group"]=> string(9) "TSTF24USD" 
                } 
            } 
            ["name"]=> array(3) { 
                ["first_name"]=> string(6) "Jason" 
                ["last_name"]=> string(5) "Test " 
                ["salutation"]=> string(0) "" 
            } 
            ["telephone"]=> array(3) { 
                ["country_code"]=> string(0) "" 
                ["area_code"]=> string(1) "0" 
                ["number"]=> string(13) "6556646446554" 
            } 
            ["source"]=> array(8) { 
                ["source"]=> string(7) "default" 
                ["btag"]=> NULL 
                ["medium"]=> string(0) "" 
                ["utm_source"]=> string(0) "" 
                ["utm_medium"]=> string(0) "" 
                ["utm_campaign"]=> string(0) "" 
                ["utm_term"]=> string(0) "" 
                ["utm_content"]=> string(0) "" 
            } 
            ["topic"]=> string(0) "" 
            ["promo_code"]=> string(0) "" 
            ["address"]=> string(13) "UnitedStates " 
            ["city"]=> string(8) "New York" 
            ["state"]=> string(1) "5" 
            ["zip"]=> string(7) "4546545" 
            ["country"]=> string(2) "HR" 
            ["language"]=> string(2) "en" 
            ["telephone_2"]=> string(0) "" 
            ["telephone_3"]=> string(0) "" 
            ["client_id_code"]=> string(0) "" 
            ["client_id_code_type"]=> string(0) "" 
            ["email_2"]=> string(0) "" 
            ["gender"]=> string(0) "" 
            ["dob"]=> int(477367200) 
            ["created_ip"]=> string(7) "0.0.0.0" 
            ["last_session"]=> string(10) "1522227864345
            ["last_session_ip"]=> string(7) "0.0.0.0" 
            ["folder"]=> string(1) "0" 
            ["compliance"]=> array(2) { 
                ["status"]=> string(8) "accepted" 
                ["files"]=> array(0) { } 
            } 
            ["owner"]=> array(4) { 
                ["username"]=> NULL 
                ["email"]=> NULL 
                ["extention"]=> NULL 
                ["team"]=> NULL 
            } 
        } 
    } 
}

PHP代码:

$result_step1 = curl_exec($ch);

$result_step1_decoded = json_decode($result_step1, true);

var_dump($result_step1_decoded);
echo '<br>';

print $result_step1_decoded->{'status'};

1 个答案:

答案 0 :(得分:2)

让我们首先格式化输出,以了解其结构

array(2) { 
    ["message"]=> string(31) "Request successfully processed." 
    ["response"]=> array(1) { 
        [954129]=> array(32) { 
            ["id"]=> string(6) "954129" 
            ["email"]=> string(31) "somemail@gmail.com" 
            ["created_on"]=> string(10) "1522224345" 
            ["type"]=> string(6) "client" 
            ["status"]=> string(3) "New" 
            ["sub_status"]=> string(11) "Unprocessed" 
            ["active"]=> string(1) "1" 
            ["accounts"]=> array(2) { 
                [0]=> array(8) { 
                    ["id"]=> string(7) "2323232" 
                    ["type"]=> string(4) "live" 
                    ["status"]=> string(6) "active" 
                    ["is_default"]=> string(1) "0" 
                    ["leverage"]=> string(2) "10" 
                    ["base_currency"]=> string(3) "USD" 
                    ["created_on"]=> string(10) "1522224478" 
                    ["group"]=> string(9) "TSTF24USD" 
                } 
                [1]=> array(8) { 
                ["id"]=> string(7) "9131428" 
                ["type"]=> string(4) "live" 
                ["status"]=> string(6) "active" 
                ["is_default"]=> string(1) "1" 
                ["leverage"]=> string(1) "1" 
                ["base_currency"]=> string(3) "USD" 
                ["created_on"]=> string(10) "1522224722310" 
                ["group"]=> string(9) "TSTF24USD" 
                } 
            } 
            ["name"]=> array(3) { 
            ["first_name"]=> string(6) "Jason" 
            ["last_name"]=> string(5) "Test " 
            ["salutation"]=> string(0) "" 
            } 
            ["telephone"]=> array(3) { 
            ["country_code"]=> string(0) "" 
            ["area_code"]=> string(1) "0" 
            ["number"]=> string(13) "6556646446554" 
            } 
            ["source"]=> array(8) { 
                ["source"]=> string(7) "default" 
                ["btag"]=> NULL 
                ["medium"]=> string(0) "" 
                ["utm_source"]=> string(0) "" 
                ["utm_medium"]=> string(0) "" 
                ["utm_campaign"]=> string(0) "" 
                ["utm_term"]=> string(0) "" 
                ["utm_content"]=> string(0) "" 
            } 
            ["topic"]=> string(0) "" 
            ["promo_code"]=> string(0) "" 
            ["address"]=> string(13) "UnitedStates " 
            ["city"]=> string(8) "New York" 
            ["state"]=> string(1) "5" 
            ["zip"]=> string(7) "4546545" 
            ["country"]=> string(2) "HR" 
            ["language"]=> string(2) "en" 
            ["telephone_2"]=> string(0) "" 
            ["telephone_3"]=> string(0) "" 
            ["client_id_code"]=> string(0) "" 
            ["client_id_code_type"]=> string(0) "" 
            ["email_2"]=> string(0) "" 
            ["gender"]=> string(0) "" 
            ["dob"]=> int(477367200) 
            ["created_ip"]=> string(7) "0.0.0.0" 
            ["last_session"]=> string(10) "1522227864345" 
            ["last_session_ip"]=> string(7) "0.0.0.0" 
            ["folder"]=> string(1) "0" 
            ["compliance"]=> array(2) { 
                ["status"]=> string(8) "accepted" 
                ["files"]=> array(0) { } 
            } 
            ["owner"]=> array(4) { 
                ["username"]=> NULL 
                ["email"]=> NULL 
                ["extention"]=> NULL 
                ["team"]=> NULL 
            } 
        } 
    } 
}

在PHP中,->用于访问对象属性。 json_decode()中的第二个参数设置为true,这意味着json_decode()返回一个关联数组,而不是对象。可以这样访问关联数组的元素:

$result_step1_decoded['response'][954129]['id'];
$result_step1_decoded['response'][954129]['email'];

或者,以下代码避免对954129值进行硬编码,该值可能会随时间变化。

$first_response = array_shift($result_step1_decoded['response']);
print $first_response['id'];
print $first_response['email'];