Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求助,json_decode含gbk字串

求助,json_decode含gbk字串

Jun 23, 2016 pm 02:20 PM

本帖最后由 novas 于 2013-09-02 20:47:19 编辑

一段curl取回的字串,完全符合json格式,格式没有问题,中文用的gbk编码,json_decode只支持utf-8,求助怎么进行正常的json_decode解析,能显示中文,谢谢各位大侠

{    "watershed": 100,    "maxPage": 1,    "currentPageNum": 1,    "comments": [        {            "append": null,            "auction": {                "aucNumId": "17813389603",                "auctionPic": "http://img.taobaocdn.com/bao/uploaded/null_40x40.jpg",                "link": "http://tradearchive.taobao.com/trade/detail/trade_snap.htm?trade_id=242542116456739",                "sku": "颜色分类:橘红色  尺码:均码(发圆通)",                "thumbnail": "",                "title": ""            },            "award": "",            "bidPriceMoney": null,            "buyAmount": 0,            "content": "因为清明放假的原因发货晚了两天 店家一再跟我说抱歉,态度非常的好 让我感觉的感动,虽然发晚了两天 但快递却很给力,很快收到了 这一款V领小开衫的颜色非常的漂亮喔 款式新,做工细致,精美,显高档, 用的面料非常不错,手感非常的舒适 ",            "date": "2013年04月10日 14:28",            "dayAfterConfirm": 0,            "enableSNS": false,            "from": "",            "lastModifyFrom": 0,            "payTime": null,            "photos": [                {                    "fileId": 343572768,                    "receiveId": 63647033326,                    "status": 1,                    "thumbnail": "http://img.taobaocdn.com/bao/uploaded/i0/343572768/T1GDd.XxRaXXaH.X6X.JPEG_40x40.jpg",                    "url": "http://img.taobaocdn.com/bao/uploaded/i0/343572768/T1GDd.XxRaXXaH.X6X.JPEG_400x400.jpg"                },                {                    "fileId": 343575722,                    "receiveId": 63647033326,                    "status": 1,                    "thumbnail": "http://img.taobaocdn.com/bao/uploaded/i2/343575722/T1hn89XwBeXXaH.X6X.JPEG_40x40.jpg",                    "url": "http://img.taobaocdn.com/bao/uploaded/i2/343575722/T1hn89XwBeXXaH.X6X.JPEG_400x400.jpg"                },                {                    "fileId": 343575732,                    "receiveId": 63647033326,                    "status": 1,                    "thumbnail": "http://img.taobaocdn.com/bao/uploaded/i0/343575732/T1UnN_XExcXXaH.X6X.JPEG_40x40.jpg",                    "url": "http://img.taobaocdn.com/bao/uploaded/i0/343575732/T1UnN_XExcXXaH.X6X.JPEG_400x400.jpg"                },                {                    "fileId": 343583345,                    "receiveId": 63647033326,                    "status": 1,                    "thumbnail": "http://img.taobaocdn.com/bao/uploaded/i1/343583345/T1vvt_XzxeXXaH.X6X.JPEG_40x40.jpg",                    "url": "http://img.taobaocdn.com/bao/uploaded/i1/343583345/T1vvt_XzxeXXaH.X6X.JPEG_400x400.jpg"                },                {                    "fileId": 343585239,                    "receiveId": 63647033326,                    "status": 1,                    "thumbnail": "http://img.taobaocdn.com/bao/uploaded/i3/343585239/T1dFh_XqlgXXaH.X6X.JPEG_40x40.jpg",                    "url": "http://img.taobaocdn.com/bao/uploaded/i3/343585239/T1dFh_XqlgXXaH.X6X.JPEG_400x400.jpg"                }            ],            "promotionType": "",            "propertiesAvg": "34500204.0",            "rate": "1",            "rateId": 63647033326,            "raterType": 0,            "reply": null,            "shareInfo": {                "lastReplyTime": "",                "pic": 0,                "reply": 0,                "share": false,                "userNumIdBase64": ""            },            "showCuIcon": false,            "showDepositIcon": false,            "spuRatting": [                {                    "desc": "45公斤",                    "name": "体重",                    "value": "45"                },                {                    "desc": "160厘米",                    "name": "身高",                    "value": "160"                },                {                    "desc": "刚好",                    "name": "是否合身",                    "value": "103500400"                }            ],            "status": 0,            "tag": "",            "useful": 0,            "user": {                "anony": false,                "avatar": "http://wwc.taobaocdn.com/avatar/getAvatar.do?userId=33743967&width=40&height=40&type=sns",                "displayRatePic": "b_blue_4.gif",                "nick": "水晶苹果jun",                "nickUrl": "http://my.taobao.com/33743967",                "rank": 2236,                "rankUrl": "http://rate.taobao.com/rate.htm?user_id=33743967&rater=1",                "userId": "33743967",                "vip": "b_red_1.gif",                "vipLevel": 4            },            "validscore": 0        },        {            "append": null,            "auction": {                "aucNumId": "17813389603",                "auctionPic": "http://img.taobaocdn.com/bao/uploaded/null_40x40.jpg",                "link": "http://tradearchive.taobao.com/trade/detail/trade_snap.htm?trade_id=209545650184382",                "sku": "颜色分类:粉红色  尺码:均码(发申通)",                "thumbnail": "",                "title": ""            },            "award": " 5.00 元店铺优惠券",            "bidPriceMoney": null,            "buyAmount": 0,            "content": " 衣服很满意 款式很小清新 天丝质地滑滑的 穿着很舒服 而且还很百搭显瘦~~怎么穿都很好看 非常喜欢!洗过一次之后也一点都不会掉色 衣服也不会变形~~经典款式 怎么都不会过时 推荐大家必须有一件~~很满意的一次网购 下次还来这家买!",            "date": "2013年04月28日 14:26",            "dayAfterConfirm": 0,            "enableSNS": false,            "from": "",            "lastModifyFrom": 0,            "payTime": null,            "photos": [                {                    "fileId": 352283912,                    "receiveId": 64430896211,                    "status": 1,                    "thumbnail": "http://img.taobaocdn.com/bao/uploaded/i0/352283912/T1iq5sXwtbXXaH.X6X.JPEG_40x40.jpg",                    "url": "http://img.taobaocdn.com/bao/uploaded/i0/352283912/T1iq5sXwtbXXaH.X6X.JPEG_400x400.jpg"                },                {                    "fileId": 352303043,                    "receiveId": 64430896211,                    "status": 1,                    "thumbnail": "http://img.taobaocdn.com/bao/uploaded/i3/352303043/T1BO5rXxVdXXaH.X6X.JPEG_40x40.jpg",                    "url": "http://img.taobaocdn.com/bao/uploaded/i3/352303043/T1BO5rXxVdXXaH.X6X.JPEG_400x400.jpg"                }            ],            "promotionType": "",            "propertiesAvg": "34500204.0",            "rate": "1",            "rateId": 64430896211,            "raterType": 0,            "reply": null,            "shareInfo": {                "lastReplyTime": "",                "pic": 0,                "reply": 0,                "share": false,                "userNumIdBase64": ""            },            "showCuIcon": false,            "showDepositIcon": false,            "spuRatting": [                {                    "desc": "49公斤",                    "name": "体重",                    "value": "49"                },                {                    "desc": "165厘米",                    "name": "身高",                    "value": "165"                },                {                    "desc": "刚好",                    "name": "是否合身",                    "value": "103500400"                }            ],            "status": 0,            "tag": "g",            "useful": 0,            "user": {                "anony": false,                "avatar": "http://wwc.taobaocdn.com/avatar/getAvatar.do?userId=751668243&width=40&height=40&type=sns",                "displayRatePic": "b_blue_1.gif",                "nick": "tb_3429463",                "nickUrl": "http://my.taobao.com/751668243",                "rank": 364,                "rankUrl": "http://rate.taobao.com/rate.htm?user_id=751668243&rater=1",                "userId": "751668243",                "vip": "",                "vipLevel": 3            },            "validscore": 0        }    ]}
Copy after login


回复讨论(解决方案)

http://blog.sina.com.cn/s/blog_6cc9c79b010131ns.html
http://www.zhisku.com/?p=92

有个疑问,你找个gbk的json串是如何生成的?
具体转换方法,很简单,把字符串转换为utf8编码,在调用json_decode既可。
具体原因可以查看博客
http://www.bo56.com/%E5%9C%A8php%E4%B8%AD%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8json_decode%E8%A7%A3%E6%9E%90gbk%E7%BC%96%E7%A0%81%E7%9A%84json%E5%AD%97%E7%AC%A6%E4%B8%B2/

这是直接从网页用curl抓取的,要是自己用json_ecode生成我也知道很简单

现在问题就是这个用gbk编码的数据已经存在了,怎样去解码

<?php$json = '"信海龙"'; //一个符合json格式的gbk编码串var_dump(json_decode($str)); //输出NULL$str = mb_convert_encoding($json, "utf8", "gbk");var_dump(json_decode($str)); //输出 string(9) "信海龙"?>
Copy after login

为什么转换为utf8编码后,就可以正常使用json_decode,参加日志
http://www.bo56.com/%E5%9C%A8php%E4%B8%AD%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8json_decode%E8%A7%A3%E6%9E%90gbk%E7%BC%96%E7%A0%81%E7%9A%84json%E5%AD%97%E7%AC%A6%E4%B8%B2/

现在问题就是这个用gbk编码的数据已经存在了,怎样去解码
那你就把数据用utf-8 编码就可以了

$s=你的json串print_r(json_decode(iconv('gbk','utf-8',$s),true));
Copy after login

晕个,写过无数种转换了,一直都没成功,现在发现原来是淘宝U站的问题,一放到外站,随便怎么写都ok能正确解码的,晕死
还是谢谢各位

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO) How do you prevent SQL Injection in PHP? (Prepared statements, PDO) Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

See all articles