問一下這個JS資料如何解碼,
問一下這個JS資料如何解碼,
資料地址:http://list.taobao.com/itemlist/default.htm?_input_charset=utf-8&json=on&cat=0&style=grid&pcat=food2011&q=ps&rr=1&spm=a2106.m5221.1000187.1&commend=all&pSize=95&_ksTS=1382516137021_144&callback=jsonp145
先聲明,用php的json_decode是不行的哦
分享到:
------解決方案--------------------
這是jsonp,給你javascript調用的 ,用jquery的jsonp方式請求,自己百度吧,或者自己寫一個
原理就是url裡面callback參數是你預先要定義的函數名
JS Bin
你如果要php請求這個連結,就把callback參數去掉
------解決方案--------------------
$url = 'http://list.taobao.com/itemlist/default.htm?_input_charset=utf-8&json=on&cat=0&style=grid&pcat=food2011&q=ps&rr=1&spm=a2106.m5221.1000187.1&commend=all&pSize=95&_ksTS=1382516137021_144&callback=jsonp145';
$s = file_get_contents($url);
preg_match('/{.+}/s', $s, $r); //他返回的是js函數,所以需要脫括弧
$o = json_decode(iconv('gbk', 'utf-8', $r[0]), true);//他的資料是gbk的,但json_decode只能處理utf-8的函數
print_r($o);
Array
(
[status] => Array
(
[code] => 200
[url] =>
)
[crumb] => Array
(
[marketInfo] => Array
(
[displayName] => 所有市場
[marketHref] => http://list.taobao.com/cat-0.htm
)
[catPathList] => Array
(
)
)
[keyword] => Array
(
[key] => q
[value] => ps
)
[sort2List] =>
[promotion] => Array
.....