2012-10-15 120 views
0

嗨即時解決這個pinterest JSON文件的問題,任何想法?感謝解析pinterest JSON API與PHP

 

    $json = file_get_contents('http://pinterestapi.co.uk/jwmoz/boards'); 

    $obj = json_decode($json); 
    foreach($obj->body as $item){ 
    $example = $item[0]->name; 
    echo $example; 
    } 

 

    { 
    "body":[ 
     {"name":"JMOZ", 
     "href":"http:\/\/pinterest.com\/jwmoz\/jmoz\/", 
     "num_of_pins":17,"cover_src":"http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920643849_2DcDfCUK_222.jpg", 
     "thumbs_src": 
      ["http:\/\/media-cache-ec5.pinterest.com\/upload\/82190761920643841_dZfvCWmE_t.jpg", 
      "http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920194573_aPAbDtHD_t.jpg", 
      "http:\/\/media-cache-ec2.pinterest.com\/upload\/82190761920194563_dQcOIHvQ_t.jpg", 
      "http:\/\/media-cache0.pinterest.com\/upload\/82190761920194557_VSSI2uQB_t.jpg" 
      ] 
     }, 
     {"name":"JMOZ", 
     "href":"http:\/\/pinterest.com\/jwmoz\/jmoz\/", 
     "num_of_pins":17,"cover_src":"http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920643849_2DcDfCUK_222.jpg", 
     "thumbs_src": 
      ["http:\/\/media-cache-ec5.pinterest.com\/upload\/82190761920643841_dZfvCWmE_t.jpg", 
      "http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920194573_aPAbDtHD_t.jpg", 
      "http:\/\/media-cache-ec2.pinterest.com\/upload\/82190761920194563_dQcOIHvQ_t.jpg", 
      "http:\/\/media-cache0.pinterest.com\/upload\/82190761920194557_VSSI2uQB_t.jpg" 
      ] 
     },  
     {"name":"JMOZ", 
     "href":"http:\/\/pinterest.com\/jwmoz\/jmoz\/", 
     "num_of_pins":17,"cover_src":"http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920643849_2DcDfCUK_222.jpg", 
     "thumbs_src": 
      ["http:\/\/media-cache-ec5.pinterest.com\/upload\/82190761920643841_dZfvCWmE_t.jpg", 
      "http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920194573_aPAbDtHD_t.jpg", 
      "http:\/\/media-cache-ec2.pinterest.com\/upload\/82190761920194563_dQcOIHvQ_t.jpg", 
      "http:\/\/media-cache0.pinterest.com\/upload\/82190761920194557_VSSI2uQB_t.jpg" 
      ] 
     }, 
     {"name":"test I\u00f1t\u00ebrn\u00e2ti\u00f4n\u00e0liz\u00e6ti\u00f8n", 
     "href":"http:\/\/pinterest.com\/jwmoz\/test-internationaliztin\/", 
     "num_of_pins":0, 
     "cover_src":false, 
     "thumbs_src":false 
     }], 
     "meta":{"count":11} 
     } 

回答

0

看來問題是您在$項目

$example = $item[0]->name; 

了[0]索引的使用這應該只是

$example = $item->name; 

要訪問縮略圖,試試

$obj = json_decode($json); 
foreach($obj->body as $item){ 
    echo '<li>' . $item->name . '<ul>'; 
    if(!empty($item->thumbs_src)) 
    { 
    foreach($item->thumbs_src as $thumbs_src){ 
     echo '<li>' . $thumbs_src . '</li>'; 
    } 
    } 
    echo '</ul></li>'; 
} 
+1

它工作完美,但不能得到「thumbs_src」,謝謝 – user1653282

+0

對不起,我編輯了我的答案,問題出在_ $ item [0] - > name_ – Scuzzy

+0

的用法完美!謝謝 – user1653282