通过php检索json yql数据

时间:2012-06-14 07:07:43

标签: php json object yql

我很难从存储在php变量中的这个json中提取必要的数据。 尚不熟悉这种数据结构...

想知道有谁知道如何让它发挥作用。

我需要遍历结果,然后阅读每个联系人 1. type = email的值 2. value-> givenName其中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"
                  }
               ]
            } 



         ]
      }
   }
}

1 个答案:

答案 0 :(得分:0)

您忘记了query。您必须以这种方式访问​​结果:

$aNative->query->results->contact[0]->created

并检查您尝试多次访问错误属性的整个代码。例如,在foreach中,您应使用$aNative->query->results->contact而不是$aNative->results,因为results它是一个对象,而不是数组。