2016-03-08 167 views
0

我有一个JSON,它具有以下结构,但我只对entries节点中的数据感兴趣,因此我的DTO类的字段镜像所述节点内的标记。但正如期望的那样,当反序列化JSON时,我得到一个错误,因为JMSserializer期望一个包含JSON中所有标记字段的对象。有没有办法可以忽略其他标签?什么是完成这个的正确方法?将JSON映射到php对象

{ 
"result": { 
    "data": { 
     "provider": "facebook", 
     "pages": { 
      "pagination": { 
       "current_page": 1, 
       "total_pages": 1, 
       "entries_per_page": 250, 
       "total_entries": 2, 
       "order": { 
        "field": "date_creation", 
        "direction": "desc" 
       } 
      }, 
      "count": 2, 
      "entries": [ 
       { 
        "user_token": "48d6b4a9-afd7-4ee7-b359-45bbf618ebe9", 
        "identity_token": "f5148587-3925-4fec-a214-3a339a023d2b", 
        "page_token": "c66c2d03-c9de-485b-9aea-445c405d44ab", 
        "date_creation": "Tue, 07 Apr 2015 10:37:52 0200", 
        "name": "Test Page", 
        "description": "This is the description of the page.", 
        "category": "Community", 
        "num_likes": 27, 
        "link": "https://www.facebook.com/test-page", 
        "thumbnailUrl": "https://graph.facebook.com/1234567890/picture?type=square", 
        "pictureUrl": "https://graph.facebook.com/1234567890/picture?type=large", 
        "source": { 
         "identifier": 1234567890, 
         "access_token": { 
          "key": "E797C0013811A1D1E35AD7EDD10FB99986DB664B0996C76ED9AE5E0A5151BBF9E797C0013811A1D1E35AD7EDD10FB99986DB664B0996C76ED9AE5E0A5151BBF9" 
         } 
        } 
       }, 
       { 
        "user_token": "1f178827-c746-43e0-84d0-75cfd0513b1e", 
        "identity_token": "462fae22-f46b-4343-838c-1fc35113e92c", 
        "page_token": "886a4c73-fa1a-4261-839f-42672f42b842", 
        "date_creation": "Tue, 07 Apr 2015 10:37:52 0200", 
        "name": "Another Test Page", 
        "description": "This is the description of the page.", 
        "category": "Computers/internet website", 
        "num_likes": 2119, 
        "link": "https://www.facebook.com/another-test-page", 
        "thumbnailUrl": "https://graph.facebook.com/987654321/picture?type=square", 
        "pictureUrl": "https://graph.facebook.com/987654321/picture?type=large", 
        "source": { 
         "identifier": 987654321, 
         "access_token": { 
          "key": "A21C619251FB098250A15A69B20BEE6ED6835149CE1496D78A674F11B0920F9FA21C619251FB098250A15A69B20BEE6ED6835149CE1496D78A674F11B0920F9F" 
         } 
        } 
       } 
      ] 
     } 
    } 
} 
} 
+0

你试过'json_decode'? – Burki

+0

@Burki我想将它映射到一个对象,这就是为什么使用JMSserializer。 – user3163473

+0

你可能还会'json_decode',然后提取你需要的部分,如果你不能提出一个更好的想法,'json_encode'是相关的部分。 – Burki

回答

0

使用json_decode()为:

$arr = json_decode($json_string, true); 
$entries = $arr['result']['data']['pages']['entries']; 
print_r($entries); 

输出:

Array 
(
    [0] => Array 
     (
      [user_token] => 48d6b4a9-afd7-4ee7-b359-45bbf618ebe9 
      [identity_token] => f5148587-3925-4fec-a214-3a339a023d2b 
      [page_token] => c66c2d03-c9de-485b-9aea-445c405d44ab 
      [date_creation] => Tue, 07 Apr 2015 10:37:52 0200 
      [name] => Test Page 
      [description] => This is the description of the page. 
      [category] => Community 
      [num_likes] => 27 
      [link] => https://www.facebook.com/test-page 
      [thumbnailUrl] => https://graph.facebook.com/1234567890/picture?type=square 
      [pictureUrl] => https://graph.facebook.com/1234567890/picture?type=large 
      [source] => Array 
       (
        [identifier] => 1234567890 
        [access_token] => Array 
         (
          [key] => E797C0013811A1D1E35AD7EDD10FB99986DB664B0996C76ED9AE5E0A5151BBF9E797C0013811A1D1E35AD7EDD10FB99986DB664B0996C76ED9AE5E0A5151BBF9 
         ) 

       ) 

     ) 

    [1] => Array 
     (
      [user_token] => 1f178827-c746-43e0-84d0-75cfd0513b1e 
      [identity_token] => 462fae22-f46b-4343-838c-1fc35113e92c 
      [page_token] => 886a4c73-fa1a-4261-839f-42672f42b842 
      [date_creation] => Tue, 07 Apr 2015 10:37:52 0200 
      [name] => Another Test Page 
      [description] => This is the description of the page. 
      [category] => Computers/internet website 
      [num_likes] => 2119 
      [link] => https://www.facebook.com/another-test-page 
      [thumbnailUrl] => https://graph.facebook.com/987654321/picture?type=square 
      [pictureUrl] => https://graph.facebook.com/987654321/picture?type=large 
      [source] => Array 
       (
        [identifier] => 987654321 
        [access_token] => Array 
         (
          [key] => A21C619251FB098250A15A69B20BEE6ED6835149CE1496D78A674F11B0920F9FA21C619251FB098250A15A69B20BEE6ED6835149CE1496D78A674F11B0920F9F 
         ) 

       ) 

     ) 

)