PHP解析var_dump json

时间:2015-08-20 07:16:49

标签: php json curl

我有一个从JSON-API获取数据的脚本。

echo var_dump(json_decode($result, true));显示:

hejarray(1) { 
  ["items"]=> array(2) {
        ["item"]=> array(1) { 
              [0]=> array(23) { 
                    ["newsdeskML"]=> string(3) "2.1"                                                                           
                    ["type_of_media"]=> string(12) "pressrelease"       
                    ["language"]=> string(2) "sv" 
                    ["source_id"]=> string(5) "47784"             
                    ["source_name"]=> string(24) "Sverige AB" 
                    ["pressroom_name"]=> string(24) "Sverige AB" 
                    ["pressroom"]=> string(2) "se" 
                    ["pressroom_id"]=> string(5) "53128"             
                    ["organization_number"]=> string(11) "556052-5833" 
                    ["id"]=> string(6) "968485" 
                    ["url"]=> string(126) "xx.com" 
                    ["published_at"]=> string(19) "2014-03-05 08:08:33"               ["created_at"]=> string(19) "2014-03-05 08:08:33"       
                    ["updated_at"]=> string(19) "2014-03-05 08:08:34"       
                    ["header"]=> string(56) "header header" 
                    ["summary"]=> string(277) "text text".........

如果我只想回复["summary"]=> string(277) "text text"中的内容,怎么办呢?

EDIT 完整的json响应,不得不删除比赛作为它的感性数据:

{  
   "items":{  
      "item":[  
         {  
            "newsdeskML":"2.1",
            "type_of_media":"pressrelease",
            "language":"sv",
            "source_id":"47784",
            "source_name":"",
            "pressroom_name":"",
            "pressroom":"se",
            "pressroom_id":"",
            "organization_number":"",
            "id":"968485",
            "url":"",
            "published_at":"2014-03-05 08:08:33",
            "created_at":"2014-03-05 08:08:33",
            "updated_at":"2014-03-05 08:08:34",
            "header":"",
            "summary":"",
            "body":""
         },
         {  
            "related_items":null
         }
      ]
   }
}

这给出了我最初发布的相同的php输出:

$ var = json_decode($ result,true); echo var_dump($ var);

但是回声var_dump($ var [' items']);给出null

2 个答案:

答案 0 :(得分:2)

你可以通过以下方式获得:

$var = json_decode($result, true);

echo $var['items']['item'][0]['summary']

答案 1 :(得分:1)

当您指定json_decode的第二个参数时,

true会返回一个关联数组(就像您一样)。所以你基本上可以选择:

$json_object = json_decode($result, true);
echo $json_object["path"]["to"]["your"]["property"];