如何使用php访问json嵌套元素

时间:2015-07-31 12:22:04

标签: php json

24小时后,我一直在努力寻找如何访问电子邮件和地址字段。我想我已经尝试了所有的组合

我的最后一次尝试

$obj = json_decode($json);
$emails = $obj->emails->address;  

JSON OUTPUT

{

"@http_status_code": 200, 
"@visible_sources": 1, 
"@available_sources": 1, 
"@search_id": "1507310336407868146373527620323253083", 
"query": {
    "emails": [
        {
            "address": "paulsparktest@googlemail.com", 
            "address_md5": "d92590f691eab834586686c563567382"
        }
    ]
}, 
"possible_persons": [
    {
        "@search_pointer": "3984f5b332b6efb3331bb137e1e97829ddff0971d9de9347cbd7fb8f82dc68de093a525a66584694339bfe580baff2aacb77954e0f154a1d0bd0a36588094972a72c1c4a63197a9736f6c425afdf66e5d8e52d35073d6499036efe9a234dd1d886f71bf54b9911a19725f118b6cd7bca521c246fe3b890a957596f8236c3c4ac5ba241198c3bdfa2f44a4e361393f1bf407130ffb5b9e2f6b1ccffca87befd0b147e51a12a54773ca31fc1a364b8cde876ca5f42b5d6f0c319f18300cab29fc1", 
        "names": [
            {
                "first": "Paul", 
                "last": "Johnson", 
                "display": "Paul Johnson"
            }
        ], 
        "usernames": [
            {
                "content": "pauljohnson111"
            }
        ], 
        "gender": {
            "@inferred": true, 
            "content": "male"
        }, 
        "addresses": [
            {
                "country": "GB", 
                "state": "ENG", 
                "city": "London", 
                "display": "London, England"
            }
        ], 
        "user_ids": [
            {
                "content": "374967130@twitter"
            }
        ], 
        "images": [
            {
                "url": "http://pbs.twimg.com/profile_images/1546592694/Roccc.jpg", 
                "thumbnail_token": "AE2861B242686E7DDBDF0D814A3486E1D19BE9609F41B4AA71B6D0FEB03454A84C36C69AC788EF676B93C5274D29CD76361050E1F057871D&dsid=39844"
            }
        ]
    }
]
}

2 个答案:

答案 0 :(得分:2)

这将为您获取地址

$obj->query->emails[0]->address;

如果你想迭代并获得其他地址

foreach ( $obj->query->emails as $email ) {
    echo $email->address;
}

答案 1 :(得分:1)

你可以得到地址&通过以下代码命名。

// Getting email
echo $data->query->emails[0]->address;

// Getting name
echo $data->possible_persons[0]->names[0]->display;

完整代码

$data = '{

"@http_status_code": 200, 
"@visible_sources": 1, 
"@available_sources": 1, 
"@search_id": "1507310336407868146373527620323253083", 
"query": {
    "emails": [
        {
            "address": "paulsparktest@googlemail.com", 
            "address_md5": "d92590f691eab834586686c563567382"
        }
    ]
}, 
"possible_persons": [
    {
        "@search_pointer": "3984f5b332b6efb3331bb137e1e97829ddff0971d9de9347cbd7fb8f82dc68de093a525a66584694339bfe580baff2aacb77954e0f154a1d0bd0a36588094972a72c1c4a63197a9736f6c425afdf66e5d8e52d35073d6499036efe9a234dd1d886f71bf54b9911a19725f118b6cd7bca521c246fe3b890a957596f8236c3c4ac5ba241198c3bdfa2f44a4e361393f1bf407130ffb5b9e2f6b1ccffca87befd0b147e51a12a54773ca31fc1a364b8cde876ca5f42b5d6f0c319f18300cab29fc1", 
        "names": [
            {
                "first": "Paul", 
                "last": "Johnson", 
                "display": "Paul Johnson"
            }
        ], 
        "usernames": [
            {
                "content": "pauljohnson111"
            }
        ], 
        "gender": {
            "@inferred": true, 
            "content": "male"
        }, 
        "addresses": [
            {
                "country": "GB", 
                "state": "ENG", 
                "city": "London", 
                "display": "London, England"
            }
        ], 
        "user_ids": [
            {
                "content": "374967130@twitter"
            }
        ], 
        "images": [
            {
                "url": "http://pbs.twimg.com/profile_images/1546592694/Roccc.jpg", 
                "thumbnail_token": "AE2861B242686E7DDBDF0D814A3486E1D19BE9609F41B4AA71B6D0FEB03454A84C36C69AC788EF676B93C5274D29CD76361050E1F057871D&dsid=39844"
            }
        ]
    }
]
}';

$data = json_decode($data);
// Getting email
echo $data->query->emails[0]->address;

// Getting name
echo $data->possible_persons[0]->names[0]->display;