php如何解析json,jsonm_decode有时可以,有时不行
网页编码GB2312(“注意这点,比较重要”),代码如下
当这里关键词为"玻璃移门"时,就无法解析json,但生成的url直接通过浏览器访问就有数据,如果关键词为"手机",就可以正常解析json,file_get_contents得到的数据貌似是utf-8的格式
<?phpprint_r(getKwToData('玻璃移门'));//当这里关键词为"玻璃移门"时,就无法解析json,但生成的url直接通过浏览器访问就有数据,如果关键词为"手机",就可以正常解析jsonfunction getKwToData($kw,$page=1,$condition=''){ if(empty($kw)){ return false; } if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$kw) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$kw) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$kw) != true) { $kw=iconv('gbk','utf-8',$kw); } $randIp='110.75.71.20'; $url="http://".$randIp."/search_turn_page_iphone.htm?sort=".$condition."&q=".urlencode($kw)."&page=".$page."&showMode=list"; $url=iconv('gbk','utf-8',$url);//生成的url $data=file_get_contents($url);//这里不管什么关键词都可以获取网页数据 $listItem=json_decode($data);//当这里关键词为"玻璃移门"时,就无法解析json,但生成的url直接通过浏览器访问就有数据,如果关键词为"手机",就可以正常解析json return $listItem->listItem;}function getLocalIp(){//获得用户ip if(isset($_SERVER['HTTP_CLIENT_IP'])){ $ip=$_SERVER['HTTP_CLIENT_IP']; }elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; }else{ $ip=$_SERVER['REMOTE_ADDR']; } return $ip;}?>
回复讨论(解决方案)
关键词为"玻璃移门"时,echo $data; 是什么值
关键词为"玻璃移门"时,echo $data; 是什么值
echo 的代码如下,代码太多只能贴出部分,由于是手动截取的listItem节点内的格式可能不正确,或者直接访问(http://110.75.71.20/search_turn_page_iphone.htm?sort=&q=%E7%8E%BB%E7%92%83%E7%A7%BB%E9%97%A8&page=1&showMode=list),注意:我php文件编码是gb2312的,echo 出来的数据在浏览器中必须选utf-8才能正常显示,否则是乱码
{"result":"true","totalPage":"100","catmap":"","ppath":"","category":"移门","auctionTagFlag1":"","auctionTagFlag2":"","auctionTagFlag3":"","listItem":[ {"name":"特价吊门。阳台厨房客厅餐厅隔断推拉门,钛合金材质玻璃隔断移门" ,"img":"http://q.i03.wimg.taobao.com/bao/uploaded/i1/10777020696035707/T12ggQXXBXXXXXXXXX_!!0-item_pic.jpg_90x90.jpg","img2":"http://q.i03.wimg.taobao.com/bao/uploaded/i1/10777020696035707/T12ggQXXBXXXXXXXXX_!!0-item_pic.jpg","iswebp":"","url":"http://a.m.taobao.com/i12812086428.htm?rn=54mFXh1-tGoPo1JS2xg7rHCf0C-90rXM9O-Y7hC&sid=aa444833fda20421","previewUrl":"http://a.m.taobao.com/ajax/pre_view.do?itemId=12812086428&sid=aa444833fda20421","favoriteUrl":"http://fav.m.taobao.com/favorite/to_collection.htm?itemNumId=12812086428&sid=aa444833fda20421", "icon":["0" ], "price":"320.00","originalPrice":"320.00","freight":"0","area":"上海","act":"月售12","itemNumId":"12812086428","nick":"特价移门淋浴房阳台", "wwimUrl":"http://im.m.taobao.com/ww/ad_ww_dialog.htm?item_num_id=12812086428&to_user=zNi829LGw8XB3NSht7%2FR9Myo","category":"50022360","isMobileEcard":"false","auctionType":"b" } , {"name":"卫生间隔断门 阳台玻璃推拉门/拼格门 钢化玻璃移门 新店特惠" ,"img":"http://q.i04.wimg.taobao.com/bao/uploaded/i4/11427033313416399/T1aPluXCdcXXXXXXXX_!!0-item_pic.jpg_90x90.jpg","img2":"http://q.i04.wimg.taobao.com/bao/uploaded/i4/11427033313416399/T1aPluXCdcXXXXXXXX_!!0-item_pic.jpg","iswebp":"","url":"http://a.m.taobao.com/i17422447064.htm?rn=54mFXh1-tGoPo1JS2xg7rHCf0C-90rXM9O-Y7hC&sid=aa444833fda20421","previewUrl":"http://a.m.taobao.com/ajax/pre_view.do?itemId=17422447064&sid=aa444833fda20421","favoriteUrl":"http://fav.m.taobao.com/favorite/to_collection.htm?itemNumId=17422447064&sid=aa444833fda20421", "icon":["0" ], "price":"218.02","originalPrice":"298.00","freight":"10","area":"郑州","act":"月售10","itemNumId":"17422447064","nick":"逸品尚家居", "wwimUrl":"http://im.m.taobao.com/ww/ad_ww_dialog.htm?item_num_id=17422447064&to_user=0t3Gt8nQvNK%2B0w%3D%3D","category":"50022360","isMobileEcard":"false","auctionType":"b" } }
你在json_decode之前将$data做下面的替换工作就可以了
$data = str_replace('\\', '\\\\', $data);
如果不知道为什么这样做,把$data输出的内容放到下面的网址下做个校验就知道了
Just Make JSON Easy!
$s = file_get_contents('http://110.75.71.20/search_turn_page_iphone.htm?sort=&q=%E7%8E%BB%E7%92%83%E7%A7%BB%E9%97%A8&page=1&showMode=list');$s = stripslashes($s);//去掉可能存在的转义$t = json_decode($s, true);//解码成数组array_walk_recursive($t, 'togbk');//因为你需要的是 gbk 编码的,所以要转一下码print_r($t);function togbk(&$item, $key) { $item = iconv('utf-8', 'gbk', $item);}
(
[result] => true
[totalPage] => 100
[catmap] =>
[ppath] =>
[category] => 移门
[auctionTagFlag1] =>
[auctionTagFlag2] =>
[auctionTagFlag3] =>
[listItem] => Array
(
[0] => Array
(
[name] => 特价吊门。阳台厨房客厅餐厅隔断推拉门,钛合金材质玻璃隔断移门
[img] => http://q.i03.wimg.taobao.com/bao/uploaded/i1/10777020696035707/T12ggQXXBXXXXXXXXX_!!0-item_pic.jpg_90x90.jpg
[img2] => http://q.i03.wimg.taobao.com/bao/uploaded/i1/10777020696035707/T12ggQXXBXXXXXXXXX_!!0-item_pic.jpg
[iswebp] =>
[url] => http://a.m.taobao.com/i12812086428.htm?rn=X3mFMh1-jGoPuafkFFhChWPExE-ER1xM9O-wi5D&sid=4d5bc9aee02ed3b0
[previewUrl] => http://a.m.taobao.com/ajax/pre_view.do?itemId=12812086428&sid=4d5bc9aee02ed3b0
[favoriteUrl] => http://fav.m.taobao.com/favorite/to_collection.htm?itemNumId=12812086428&sid=4d5bc9aee02ed3b0
[icon] => Array
(
[0] => 0
)
[price] => 320.00
[originalPrice] => 320.00
[freight] => 0
[area] => 上海
[act] => 月售12
[itemNumId] => 12812086428
[nick] => 特价移门淋浴房阳台
[wwimUrl] => http://im.m.taobao.com/ww/ad_ww_dialog.htm?item_num_id=12812086428&to_user=zNi829LGw8XB3NSht7%2FR9Myo
[category] => 50022360
[isMobileEcard] => false
[auctionType] => b
)
[1] => Array
(
[name] => 卫生间隔断门 阳台玻璃推拉门/拼格门 钢化玻璃移门 新店特惠
[img] => http://q.i04.wimg.taobao.com/bao/uploaded/i4/11427033313416399/T1aPluXCdcXXXXXXXX_!!0-item_pic.jpg_90x90.jpg
......
你难道不知道json不支持gbk编码么?虽然不会报错也不会出现乱码,但官方已经声明不支持gbk。所以就出现你说的这种情况了。
我来总结一番:
1.json不支持gbk编码;
2.array_walk_recursive($t, 'togbk');//因为你需要的是 gbk 编码的,所以要转一下码
php中json_decode();解析json数据,如果json里有中文数据,是解析不了的。
stripslashes($abc);//去掉可能存在的转义

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov
