2014-07-21 82 views
-1

我正在我的網站上實現uploads.im api。它運作良好和圖片被上傳,但我得到一個JSON響應,如..JSON對數據庫的響應

{"status_code":200,"status_txt":"OK","data":{"img_name":"8zN9G.jpg","img_url":"http:\/\/s1.uploads.im\/8zN9G.jpg","img_view":"http:\/\/uploads.im\/8zN9G.jpg","img_width":"954","img_height":"1421","img_attr":"width=\"954\" height=\"1421\"","img_size":"327.8 KB","img_bytes":335711,"thumb_url":"http:\/\/s1.uploads.im\/t\/8zN9G.jpg","thumb_width":360,"thumb_height":536,"source":"base64 image string","resized":"0","delete_key":"8c9bd7ab84a7bd6f"}}

和不知道如何將這些數據分析數據庫。我想在我的數據庫中插入上傳圖片的網址。請幫幫我。

+0

請提供您已經嘗試了一些什麼信息,或者至少是在上下文。例如,不清楚你是否想用PHP處理這個JSON代碼(我只能根據標籤猜測)。 – ljacqu

回答

1

可以使用json_decode

$response = '{"status_code":200,"status_txt":"OK","data":{"img_name":"8zN9G.jpg","img_url":"http:\/\/s1.uploads.im\/8zN9G.jpg","img_view":"http:\/\/uploads.im\/8zN9G.jpg","img_width":"954","img_height":"1421","img_attr":"width=\"954\" height=\"1421\"","img_size":"327.8 KB","img_bytes":335711,"thumb_url":"http:\/\/s1.uploads.im\/t\/8zN9G.jpg","thumb_width":360,"thumb_height":536,"source":"base64 image string","resized":"0","delete_key":"8c9bd7ab84a7bd6f"}}'; 

$arr = json_decode($response, true); 
echo "<pre>"; 
print_r($arr); 
echo $arr['data']['img_url']; 

OUPUT:

Array 
(
    [status_code] => 200 
    [status_txt] => OK 
    [data] => Array 
     (
      [img_name] => 8zN9G.jpg 
      [img_url] => http://s1.uploads.im/8zN9G.jpg 
      [img_view] => http://uploads.im/8zN9G.jpg 
      [img_width] => 954 
      [img_height] => 1421 
      [img_attr] => width="954" height="1421" 
      [img_size] => 327.8 KB 
      [img_bytes] => 335711 
      [thumb_url] => http://s1.uploads.im/t/8zN9G.jpg 
      [thumb_width] => 360 
      [thumb_height] => 536 
      [source] => base64 image string 
      [resized] => 0 
      [delete_key] => 8c9bd7ab84a7bd6f 
     ) 

) 
http://s1.uploads.im/8zN9G.jpg