PHP JSON:从Yahoo JSON响应联系对象中读取联系人详细信息

时间:2013-08-06 05:14:35

标签: php oauth json yahoo-api

将Yahoo Contact API集成到我的网站后,我开始将用户联系人列表作为Yahoo JSON响应联系对象。

以下是回复格式:

{"contacts":{"start":0,"count":5,"total":110,"contact":[{"isConnection":false,"id":16777250,"fields":[{"id":16777292,"type":"yahooid","value":"ahmed_love7353","editedBy":"OWNER","categories":[]},{"id":16777291,"type":"name","value":{"givenName":"ahmed","middleName":"","familyName":"_love","prefix":"","suffix":"","givenNameSound":"","familyNameSound":""},"editedBy":"OWNER","categories":[]}]},{"isConnection":false,"id":52,"fields":[{"id":124,"type":"email","value":"15688708283341592078@mail.orkut.com","editedBy":"OWNER","categories":[]}]},{"isConnection":false,"id":16777271,"fields":[{"id":16777343,"type":"email","value":"abcdall4u@gmail.com","editedBy":"OWNER","categories":[]}]},{"isConnection":false,"id":31,"fields":[{"id":69,"type":"yahooid","value":"supriya_9392","editedBy":"OWNER","categories":[]},{"id":70,"type":"name","value":{"givenName":"Supriya","middleName":"","familyName":"Agarwal","prefix":"","suffix":"","givenNameSound":"","familyNameSound":""},"editedBy":"OWNER","categories":[]}]},{"isConnection":false,"id":23,"fields":[{"id":45,"type":"phone","value":"09470657255","editedBy":"OWNER","flags":["MOBILE"],"categories":[]},{"id":44,"type":"name","value":{"givenName":"akhilesh","middleName":"brother()","familyName":"","prefix":"","suffix":"","givenNameSound":"","familyNameSound":""},"editedBy":"OWNER","categories":[]}]}]}}

问题:

无法从上面的代码中读取(获取)givenName,familyName和电子邮件。

尝试:

使用json_decode();但我再也无法阅读。

以下是解码格式:

stdClass Object ( [contacts] => stdClass Object ( [start] => 0 [count] => 5 [total] => 110 [contact] => Array ( [0] => stdClass Object ( [isConnection] => [id] => 16777250 [fields] => Array ( [0] => stdClass Object ( [id] => 16777292 [type] => yahooid [value] => ahmed_love7353 [editedBy] => OWNER [categories] => Array ( ) ) [1] => stdClass Object ( [id] => 16777291 [type] => name [value] => stdClass Object ( [givenName] => ahmed [middleName] => [familyName] => _love [prefix] => [suffix] => [givenNameSound] => [familyNameSound] => ) [editedBy] => OWNER [categories] => Array ( ) ) ) ) [1] => stdClass Object ( [isConnection] => [id] => 52 [fields] => Array ( [0] => stdClass Object ( [id] => 124 [type] => email [value] => 15688708283341592078@mail.orkut.com [editedBy] => OWNER [categories] => Array ( ) ) ) ) [2] => stdClass Object ( [isConnection] => [id] => 16777271 [fields] => Array ( [0] => stdClass Object ( [id] => 16777343 [type] => email [value] => abcdall4u@gmail.com [editedBy] => OWNER [categories] => Array ( ) ) ) ) [3] => stdClass Object ( [isConnection] => [id] => 31 [fields] => Array ( [0] => stdClass Object ( [id] => 69 [type] => yahooid [value] => supriya_9392 [editedBy] => OWNER [categories] => Array ( ) ) [1] => stdClass Object ( [id] => 70 [type] => name [value] => stdClass Object ( [givenName] => Supriya [middleName] => [familyName] => Agarwal [prefix] => [suffix] => [givenNameSound] => [familyNameSound] => ) [editedBy] => OWNER [categories] => Array ( ) ) ) ) [4] => stdClass Object ( [isConnection] => [id] => 23 [fields] => Array ( [0] => stdClass Object ( [id] => 45 [type] => phone [value] => 09470657255 [editedBy] => OWNER [flags] => Array ( [0] => MOBILE ) [categories] => Array ( ) ) [1] => stdClass Object ( [id] => 44 [type] => name [value] => stdClass Object ( [givenName] => akhilesh [middleName] => brother() [familyName] => [prefix] => [suffix] => [givenNameSound] => [familyNameSound] => ) [editedBy] => OWNER [categories] => Array ( ) ) ) ) ) ) ) 

问题:

  • 如何以简单的方式阅读givenName,familyName和电子邮件?

  • 让它更具可读性的最佳方法是什么?

注意:我是JSON的新手。

1 个答案:

答案 0 :(得分:2)

试试这个:

<?php
$json = '{"contacts":{"start":0,"count":5,"total":110,"contact":[{"isConnection":false,"id":16777250,"fields":[{"id":16777292,"type":"yahooid","value":"ahmed_love7353","editedBy":"OWNER","categories":[]},{"id":16777291,"type":"name","value":{"givenName":"ahmed","middleName":"","familyName":"_love","prefix":"","suffix":"","givenNameSound":"","familyNameSound":""},"editedBy":"OWNER","categories":[]}]},{"isConnection":false,"id":52,"fields":[{"id":124,"type":"email","value":"15688708283341592078@mail.orkut.com","editedBy":"OWNER","categories":[]}]},{"isConnection":false,"id":16777271,"fields":[{"id":16777343,"type":"email","value":"abcdall4u@gmail.com","editedBy":"OWNER","categories":[]}]},{"isConnection":false,"id":31,"fields":[{"id":69,"type":"yahooid","value":"supriya_9392","editedBy":"OWNER","categories":[]},{"id":70,"type":"name","value":{"givenName":"Supriya","middleName":"","familyName":"Agarwal","prefix":"","suffix":"","givenNameSound":"","familyNameSound":""},"editedBy":"OWNER","categories":[]}]},{"isConnection":false,"id":23,"fields":[{"id":45,"type":"phone","value":"09470657255","editedBy":"OWNER","flags":["MOBILE"],"categories":[]},{"id":44,"type":"name","value":{"givenName":"akhilesh","middleName":"brother()","familyName":"","prefix":"","suffix":"","givenNameSound":"","familyNameSound":""},"editedBy":"OWNER","categories":[]}]}]}}';
$obj = json_decode($json);
foreach ($obj->contacts->contact as $contact) {
    if (isset($contact->fields[1]->value->givenName)) {
        echo $contact->fields[1]->value->givenName . '<br>';
    }

    if (isset($contact->fields[0]->type) && $contact->fields[0]->type == 'email') {
        echo $contact->fields[0]->value . '<br>';
    }
}
?>

<强> Demo Link