我正在努力从存储在 php 变量中的这个 json 中提取必要的数据。还不是这种数据结构的专家...
想知道任何人都知道如何使它工作。
我需要遍历结果,然后读取每个联系人以获得 1. type=email 的值 2. value->givenName where type=name (只要元素存在)
json解码值如下
$aNative = json_decode($retval);
echo "lang: " . $aNative->query->lang . "<br>"; (works)
(when go deeper, fail to retrieve any value...)
echo "created: " . $aNative->results->contact[0]->created . "<br>";
foreach($aNative->$query->results as $contact)
{
echo $contact->id;
echo $contact->fields[0]->type;
if($contact->fields[0]->type=="email")
{
echo $contact->fields[0]->value;
}
}
完整的json:
{
"query":{
"count":10,
"created":"2012-06-13T09:13:45Z",
"lang":"en-US",
"results":{
"contact":[
{
"created":"2006-10-29T04:32:07Z",
"updated":"2006-10-29T04:32:07Z",
"uri":"http://social.yahooapis.com/v1/user/BNQJOMYDQYLY2FZSMROV4QDA6MI/contact/14",
"isConnection":"false",
"id":"14",
"fields":{
"created":"2006-10-29T04:32:07Z",
"updated":"2006-10-29T04:32:07Z",
"uri":"http://social.yahooapis.com/v1/user/BNQJOMYDQYLY2FZSMROV4QDA6MI/contact/14/email/11",
"id":"11",
"type":"email",
"value":"angBB@gmail.com",
"editedBy":"OWNER"
}
},
{
"created":"2006-10-29T04:32:07Z",
"updated":"2006-10-29T04:32:07Z",
"uri":"http://social.yahooapis.com/v1/user/MNQJOMYDQYLY2FZSMROV4QDA6MI/contact/12",
"isConnection":"false",
"id":"12",
"fields":[
{
"created":"2006-10-29T04:32:07Z",
"updated":"2006-10-29T04:32:07Z",
"uri":"http://social.yahooapis.com/v1/user/MNQJOMYDQYLY2FZSMROV4QDA6MI/contact/12/email/6",
"id":"6",
"type":"email",
"value":"jaq@lifemastr.com",
"editedBy":"OWNER"
},
{
"created":"2006-10-29T04:32:07Z",
"updated":"2006-10-29T04:32:07Z",
"uri":"http://social.yahooapis.com/v1/user/MNQJOMYDQYLY2FZSMROV4QDA6MI/contact/12/name/5",
"id":"5",
"type":"name",
"value":{
"givenName":"'Jaq'",
"middleName":null,
"familyName":null,
"prefix":null,
"suffix":null,
"givenNameSound":null,
"familyNameSound":null
},
"editedBy":"OWNER"
}
]
}
]
}
}
}