JSON对数据库的响应

时间:2014-07-21 11:00:35

标签: php mysql json database upload

我正在我的网站上实现uploads.im api。它运行良好,图像上传,但我收到了像...这样的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"}}

并且不知道如何将此数据解析到数据库。我想在我的数据库中插入上传图片的网址。请帮帮我。

1 个答案:

答案 0 :(得分: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'];

输出:

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