Question

After integrating Yahoo Contact API to my website, I am start getting user contact list as Yahoo JSON response Contact object.

Following are the response format:

{"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":[]}]}]}}

Problem:

Not able to read(fetch) givenName, familyName and email from above code.

Tried:

Used json_decode(); but again i am not able to read the same.

following is decoded format:

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 ( ) ) ) ) ) ) ) 

Question:

  • How can i read givenName, familyName and email in a easy way?

  • What is the best way to make it more readable?

Note: I am new with JSON.

Was it helpful?

Solution

Try this:

<?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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top