search
HomeBackend DevelopmentPHP Tutorialphp json 不能解析中文字符串???

今天遇到一个很奇怪的问题,在使用json_decode的时候,遇到一个奇怪的问题,在遇到中文字符组的时候,怎么都解析不了,把这个中文的字段删除,里面就能解析了....搞了半天不知道哪里的问题,路过的大虾帮忙看看

	$nodeAry=array();	while($row=mssql_fetch_array($rs)){		$description=strstr("//","",$row['description']);	   array_push($nodeAry,'{"fchrItemID":"'.$row["fchrItemID"].'","fchrItemTypeID":"'.$row["fchrItemTypeID"].'","fchrItemName":"'.$row["fchrItemName"].'","flotQuotePrice":"'.$row["flotQuotePrice"].'","description":"'.trim($description).'","fdtmLastModifyTime":"'.$row["fdtmLastModifyTime"].'","sales_volume":"'.$row["sales_volume"].'"}');    }	$tbstr=implode(',',$nodeAry);   ?>{"table":[<?=$tbstr;?>]}


这里的description 字段有中文,只要把这个字段去掉就可以了,

 header("Content-Type:text/html; charset=utf-8");		$ch = curl_init();	curl_setopt($ch, CURLOPT_URL, $remoteAPIBasePath . "sync2shop_api/item.php");	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);	curl_setopt($ch, CURLOPT_HEADER, 0);	$output = curl_exec($ch);		curl_close($ch);		$obj=json_decode(iconv("gb2312","utf-8",$output));	var_dump( $obj);	//$obj=json_decode($output);	$rscount=count($obj->table);		$han=fopen("debug.php","a");	fwrite($han,$output."---------".$rscount);	fclose($han);


我是这么解析的,不知道哪里有错误,有谁帮忙看看!


回复讨论(解决方案)

php 的 json 函数只对 utf-8 编码的的字符串有效

你需要先将字符串转成 utf-8 编码的,然后再解码

只能说你拿到的 json 不合规范,中文应该都是 \uxxxx 形式

已经设置编码utf-8 编码了,但是还是不行啊

只能说你拿到的 json 不合规范,中文应该都是 \uxxxx 形式

数据取出的数据怎么会有反斜杠呢?自己加一个么?

我的数据是从远程数据库取的,难道会有影响吗?

你最好贴出你的数据,只要格式正确。不会有问题的

$s = '{"a":"我的","b":"数据","c":["是从远程数据库","取的"],"d":"难道会有影响吗?"}';$s = iconv('gbk', 'utf-8', $s);print_r(json_decode($s));
stdClass Object(    [a] => 我的    [b] => 数据    [c] => Array        (            [0] => 是从远程数据库            [1] => 取的        )    [d] => 难道会有影响吗?)

你最好贴出你的数据,只要格式正确。不会有问题的

$s = '{"a":"我的","b":"数据","c":["是从远程数据库","取的"],"d":"难道会有影响吗?"}';$s = iconv('gbk', 'utf-8', $s);print_r(json_decode($s));
stdClass Object(    [a] => 我的    [b] => 数据    [c] => Array        (            [0] => 是从远程数据库            [1] => 取的        )    [d] => 难道会有影响吗?)




我只截取一部分,数据太多了,麻烦帮忙看看呐!

"table":[{"fchrItemID":"1E7D5286-7379-4B6E-B63C-0081596B808F","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222S13","flotQuotePrice":"499.000000","description":"高密度的丝光棉面料,手工钉珠的人像艺术款。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"E82E5A29-A1BD-4F53-AD65-0147906E224D","fchrItemTypeID":"73A4E44D-7BF7-49CF-9A75-DCC805D93FDB","fchrItemName":"H13143C04","flotQuotePrice":"799.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"A7FD2243-D39C-49D5-9785-01DD923FA60E","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C04","flotQuotePrice":"499.000000","description":"丝光棉显示高档感,图形用胶印和钉珠的复杂工艺而非普通大众品牌较多使用的平面印花印制,彰显独特的设计点。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"4E82D694-CA4C-4134-92BA-027EE10B9100","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C35","flotQuotePrice":"499.000000","description":"高级丝光棉,蜻蜓为元素的创意有趣的图案和品牌logo和金属贴片的再组合方式呈现艺术感的品牌特性。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"6D129F91-1869-47BE-9AED-02A0E156B120","fchrItemTypeID":"6DC7F007-C0EB-45FE-94F1-4C792B8FEB23","fchrItemName":"H13456C14","flotQuotePrice":"3899.000000","description":"","fdtmLastModifyTime":"","sales_volume":"0"},{"fchrItemID":"6FA0B24C-6C17-43BB-ACAE-04369F73FE8D","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C21","flotQuotePrice":"499.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"A875F37E-503E-4F25-BCEC-04BA7A53D18F","fchrItemTypeID":"6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA","fchrItemName":"H13111C13","flotQuotePrice":"599.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"4C3E6DFB-F4CE-404D-930D-04BE6591965F","fchrItemTypeID":"6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA","fchrItemName":"H13111C12","flotQuotePrice":"699.000000","description":"高支高密的全棉面料,前胸对称的色块拼接设计款。","fdtmLastModifyTime":"","sales_volume":""},

人呢。。。。

$s =<<< TXT{"table":[{"fchrItemID":"1E7D5286-7379-4B6E-B63C-0081596B808F","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222S13","flotQuotePrice":"499.000000","description":"高密度的丝光棉面料,手工钉珠的人像艺术款。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"E82E5A29-A1BD-4F53-AD65-0147906E224D","fchrItemTypeID":"73A4E44D-7BF7-49CF-9A75-DCC805D93FDB","fchrItemName":"H13143C04","flotQuotePrice":"799.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"A7FD2243-D39C-49D5-9785-01DD923FA60E","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C04","flotQuotePrice":"499.000000","description":"丝光棉显示高档感,图形用胶印和钉珠的复杂工艺而非普通大众品牌较多使用的平面印花印制,彰显独特的设计点。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"4E82D694-CA4C-4134-92BA-027EE10B9100","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C35","flotQuotePrice":"499.000000","description":"高级丝光棉,蜻蜓为元素的创意有趣的图案和品牌logo和金属贴片的再组合方式呈现艺术感的品牌特性。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"6D129F91-1869-47BE-9AED-02A0E156B120","fchrItemTypeID":"6DC7F007-C0EB-45FE-94F1-4C792B8FEB23","fchrItemName":"H13456C14","flotQuotePrice":"3899.000000","description":"","fdtmLastModifyTime":"","sales_volume":"0"},{"fchrItemID":"6FA0B24C-6C17-43BB-ACAE-04369F73FE8D","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C21","flotQuotePrice":"499.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"A875F37E-503E-4F25-BCEC-04BA7A53D18F","fchrItemTypeID":"6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA","fchrItemName":"H13111C13","flotQuotePrice":"599.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"4C3E6DFB-F4CE-404D-930D-04BE6591965F","fchrItemTypeID":"6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA","fchrItemName":"H13111C12","flotQuotePrice":"699.000000","description":"高支高密的全棉面料,前胸对称的色块拼接设计款。","fdtmLastModifyTime":"","sales_volume":""}]}TXT;$s = iconv('gbk', 'utf-8', $s);print_r(json_decode($s));
stdClass Object
(
    [table] => Array
        (
            [0] => stdClass Object
                (
                    [fchrItemID] => 1E7D5286-7379-4B6E-B63C-0081596B808F
                    [fchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
                    [fchrItemName] => H13222S13
                    [flotQuotePrice] => 499.000000
                    [description] => 高密度的丝光棉面料,手工钉珠的人像艺术款。
                    [fdtmLastModifyTime] => 
                    [sales_volume] => 
                )

            [1] => stdClass Object
                (
                    [fchrItemID] => E82E5A29-A1BD-4F53-AD65-0147906E224D
                    [fchrItemTypeID] => 73A4E44D-7BF7-49CF-9A75-DCC805D93FDB
                    [fchrItemName] => H13143C04
                    [flotQuotePrice] => 799.000000
                    [description] => 
                    [fdtmLastModifyTime] => 
                    [sales_volume] => 
                )

            [2] => stdClass Object
                (
                    [fchrItemID] => A7FD2243-D39C-49D5-9785-01DD923FA60E
                    [fchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
                    [fchrItemName] => H13222C04
                    [flotQuotePrice] => 499.000000
                    [description] => 丝光棉显示高档感,图形用胶印和钉珠的复杂工艺而非普通大众品牌较多使用的平面印花印制,彰显独特的设计点。
                    [fdtmLastModifyTime] => 
                    [sales_volume] => 
                )

            [3] => stdClass Object
                (
                    [fchrItemID] => 4E82D694-CA4C-4134-92BA-027EE10B9100
                    [fchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
                    [fchrItemName] => H13222C35
                    [flotQuotePrice] => 499.000000
                    [description] => 高级丝光棉,蜻蜓为元素的创意有趣的图案和品牌logo和金属贴片的再组合方式呈现艺术感的品牌特性。
                    [fdtmLastModifyTime] => 
                    [sales_volume] => 
                )

            [4] => stdClass Object
                (
                    [fchrItemID] => 6D129F91-1869-47BE-9AED-02A0E156B120
                    [fchrItemTypeID] => 6DC7F007-C0EB-45FE-94F1-4C792B8FEB23
                    [fchrItemName] => H13456C14
                    [flotQuotePrice] => 3899.000000
                    [description] => 
                    [fdtmLastModifyTime] => 
                    [sales_volume] => 0
                )

            [5] => stdClass Object
                (
                    [fchrItemID] => 6FA0B24C-6C17-43BB-ACAE-04369F73FE8D
                    [fchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
                    [fchrItemName] => H13222C21
                    [flotQuotePrice] => 499.000000
                    [description] => 
                    [fdtmLastModifyTime] => 
                    [sales_volume] => 
                )

            [6] => stdClass Object
                (
                    [fchrItemID] => A875F37E-503E-4F25-BCEC-04BA7A53D18F
                    [fchrItemTypeID] => 6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA
                    [fchrItemName] => H13111C13
                    [flotQuotePrice] => 599.000000
                    [description] => 
                    [fdtmLastModifyTime] => 
                    [sales_volume] => 
                )

            [7] => stdClass Object
                (
                    [fchrItemID] => 4C3E6DFB-F4CE-404D-930D-04BE6591965F
                    [fchrItemTypeID] => 6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA
                    [fchrItemName] => H13111C12
                    [flotQuotePrice] => 699.000000
                    [description] => 高支高密的全棉面料,前胸对称的色块拼接设计款。
                    [fdtmLastModifyTime] => 
                    [sales_volume] => 
                )

        )

)

能帮我远程弄一下么? 我用了curl_init(),curl_exec() ,输出流,获取的 

$ch = curl_init();	curl_setopt($ch, CURLOPT_URL, $remoteAPIBasePath . "sync2shop_api/item.php");	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);	curl_setopt($ch, CURLOPT_HEADER, 0);	$output = curl_exec($ch);		curl_close($ch);			$obj=json_decode(iconv("gb2312","utf-8",$output));	
 

能帮我远程弄一下么? 我用了curl_init(),curl_exec() ,输出流,获取的 

$ch = curl_init();	curl_setopt($ch, CURLOPT_URL, $remoteAPIBasePath . "sync2shop_api/item.php");	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);	curl_setopt($ch, CURLOPT_HEADER, 0);	$output = curl_exec($ch);		curl_close($ch);			$obj=json_decode(iconv("gb2312","utf-8",$output));	
 


这里的$output 打印出来就是json的东西,不是直接写在里面的 

如果向你这么写,为啥我的会报错呢?

 header("Content-Type:text/html; charset=utf-8");		$ch = curl_init();	curl_setopt($ch, CURLOPT_URL, $remoteAPIBasePath . "sync2shop_api/item.php");	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);	curl_setopt($ch, CURLOPT_HEADER, 0);	$output = curl_exec($ch);	curl_close($ch);		$obj=<<< TXT var_dump($output) TXT;	$obj=json_decode(iconv("gb2312","utf-8",$output));		echo count($obj->table);



程序文件保存为UTF-8编码。
item.php那个文件的字符集是什么?是否有转码的必要?
一般从GBK转UTF-8比较好。

如果想用json_decode解析gbk编码。很容易。只要转换为utf8编码既可。
具体实现和原因可以查看这篇日志
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/

在php中如何使用json_decode解析gbk编码的json字符串

程序文件保存为UTF-8编码。
item.php那个文件的字符集是什么?是否有转码的必要?
一般从GBK转UTF-8比较好。

item 文件是utf-8 的

$output 是你用 curl 从 item.php 获取的
而 item.php 是 utf-8 的
那么
首先检查 item.php 及其相关的文件是否有 BOM 头
其次检查数据库读出的内容是否为 utf-8 的

$output 是你用 curl 从 item.php 获取的
而 item.php 是 utf-8 的
那么
首先检查 item.php 及其相关的文件是否有 BOM 头
其次检查数据库读出的内容是否为 utf-8 的

从output 看 utf-8 的,item 文件没有bom ,
但是用json_decode($output ) ,发现count 是0 ,应该还是解析json 的问题   

$obj=json_decode(iconv("gb2312","utf-8",$output));
    var_dump( $obj);
输出$obj看下,看是否是一个完整的json格式。

$output = curl_exec($ch);
取到 $output 后
echo base64_encode(substr($output, 0, 200));
贴出结果

$output = curl_exec($ch);
取到 $output 后
echo base64_encode(substr($output, 0, 200));
贴出结果


无语了

http://218.85.194.210:8888/sync2shop_api/item.php   ,这里是远程的json 数据

你在改数据?

你在改数据?

我在同步远程的数据,往本地数据库插入啊,远程的json 传过来,解析不了,本地也就没东西了

现在从 http://218.85.194.210:8888/sync2shop_api/item.php 得到的数据中已经已经没有中文了

item.php是远程的,utf-8,那么你获取的item.php里的数据也是UTF-8的,应该没有转码的必要了。

先urlencode在json然后urldecode

问题已经解决,谢谢各位的帮助!

我是用了加密解密函数 ,实现的json 传送的...

Statement
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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

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-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

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.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

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' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

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

How to Register and Use Laravel Service ProvidersHow to Register and Use Laravel Service ProvidersMar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools