How PHP can parse Json,jsonm_decode sometimes, sometimes not

Source: Internet
Author: User
This post was last edited by cnaaa8 on 2013-06-21 09:55:14

Page encoding GB2312 ("Note this, more important"), the code is as follows
When the keyword here is "glass sliding door", the JSON can not be parsed, but the generated URL directly through the browser access to data, if the keyword is "mobile phone", you can normally parse json,file_get_contents the resulting data seems to be utf-8 format

 ListItem;} function Getlocalip () {//Get user ipif (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;}? >


Reply to discussion (solution)

When the key word is "glass sliding door", echo $data; What is the value

When the key word is "glass sliding door", echo $data; What is the value

Echo's code is as follows, the code is too much to post only part, because the manual interception of the ListItem node within the format may be incorrect, or direct access (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), note: My php file encoding is gb2312, echo Out of the data in the browser must be selected Utf-8 to normal display, otherwise garbled

{"Result": "true", "totalpage": "+", "Catmap": "", "Ppath": "", "category": "Move Gate", "AuctionTagFlag1": "", " AuctionTagFlag2 ":" "," AuctionTagFlag3 ":" "," ListItem ": [{" Name ":" Special price hanging door. "] Balcony Kitchen Living room restaurant partition sliding door, titanium material glass partition sliding gate "," 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 ":" Shanghai "," act ":" Monthly Sale "," Itemnumid ":" 12812086428 "," Nick ":" Special sliding door shower room balcony "," 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 ":" Bathroom partition door balcony glass sliding door/mosaic door Tempered glass sliding door new store Special "," 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 ":" Ten "," area ":" Zhengzhou "," act ":" Monthly Sales "," Itemnumid ":" 17422447064 "," Nick ":" Yi Shang Home "," 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 "}} 

You can do the following $data before Json_decode.

$data = str_replace (' \ \ ', ' \\\\ ', $data);

If you do not know why to do so, put the contents of the $data output below the URL to do a check to know
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);//Remove the possible escape $t = Json_decode ($s, true);//decode array Array_ Walk_recursive ($t, ' TOGBK ');//Because what you need is GBK encoded, so turn the code print_r ($t), function TOGBK (& $item, $key) {  $item = Iconv ( ' Utf-8 ', ' GBK ', $item);}
Array
(
[Result] = True
[Totalpage] = 100
[Catmap] =
[Ppath] =
[Category] = Sliding door
[AUCTIONTAGFLAG1] =
[AuctionTagFlag2] =
[AUCTIONTAGFLAG3] =
[ListItem] = = Array
(
[0] = = Array
(
[Name] + special elevator door. Balcony Kitchen Living room restaurant partition sliding door, titanium alloy material glass partition sliding Doors
[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] = Shanghai
[act] = $12/month
[Itemnumid] = 12812086428
[Nick] = special sliding door shower room balcony
[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] = Bathroom partition door balcony glass sliding door/panel door tempered glass sliding doors new store special
[img] = http://q.i04.wimg.taobao.com/bao/uploaded/i4/11427033313416399/T1aPluXCdcXXXXXXXX_!!0-item_pic.jpg_90x90.jpg
......

Don't you know that JSON doesn't support GBK encoding? Although there will be no error or garbled, but the official has declared that it does not support GBK. So that's what you're talking about.

Let me summarize:
1.json GBK encoding is not supported;
2.array_walk_recursive ($t, ' TOGBK ');//Because what you need is GBK encoded, so turn the code.

PHP Json_decode (); Parse JSON data, if there is Chinese data in JSON, it is unable to parse.

Stripslashes ($ABC);//Remove possible escapes

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.