php json_encode 数据

php中文网
发布: 2016-08-08 09:20:24
原创
1319人浏览过
json_encode() 输出数据只认识UTF-8,所有在输出数据时,注意数据编码格式!!! 

解决方案:

If you have an ANSI encoded string, using utf8_encode() is the wrong function to deal with this. You need to properly convert it from ANSI to UTF-8 first. That will certainly reduce the number of Unicode escape sequences like \u0082 from the json output, but technically these sequences are valid for json, you must not fear them.

Converting ANSI to UTF-8 with PHPjson_encode works with UTF-8 encoded strings only. If you need to create valid json successfully from an ANSI encoded string, you need to re-encode/convert it to UTF-8 first. Then json_encodewill just work as documented.To convert an encoding from ANSI (more correctly I assume you have a Windows-1252 encoded string, which is popular but wrongly referred to as ANSI) to UTF-8 you can make use of the mb_convert_encoding() function:
<codeLucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif; white-space:inherit"><span>$str </span><span>=</span><span> mb_convert_encoding</span><span>(</span><span>$str</span><span>,</span><span></span><span>"UTF-8"</span><span>,</span><span></span><span>"Windows-1252"</span><span>);</span></code>
登录后复制
Another function in PHP that can convert the encoding / charset of a string is called iconv based onlibiconv. You can use it as well:
<codeLucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif; white-space:inherit"><span>$str </span><span>=</span><span> iconv</span><span>(</span><span>"CP1252"</span><span>,</span><span></span><span>"UTF-8"</span><span>,</span><span> $str</span><span>);</span></code>
登录后复制
Note on utf8_encode()utf8_encode() does only work for Latin-1, not for ANSI. So you will destroy part of your characters inside that string when you run it through that function.Related: What is ANSI format?For a more fine-grained control of what json_encode() returns, see the list of predifined constants(PHP version dependent, incl. PHP 5.4, some constants remain undocumented and are available in the source code only so far).Changing the encoding of an array/iteratively (PDO comment)As you wrote in a comment that you have problems to apply the function onto an array, here is some code example. It's always needed to first change the encoding before using json_encode. That's just a standard array operation, for the simpler case of pdo::fetch() a foreach iteration:
<codeLucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif; white-space:inherit"><span>while</span><span>(</span><span>$row </span><span>=</span><span> $q</span><span>-></span><span>fetch</span><span>(</span><span>PDO</span><span>::</span><span>FETCH_ASSOC</span><span>))</span><span></span><span>{</span><span></span><span>foreach</span><span>(</span><span>$row </span><span>as</span><span></span><span>&</span><span>$value</span><span>)</span><span></span><span>{</span><span>
    $value </span><span>=</span><span> mb_convert_encoding</span><span>(</span><span>$value</span><span>,</span><span></span><span>"UTF-8"</span><span>,</span><span></span><span>"Windows-1252"</span><span>);</span><span></span><span>}</span><span>
  unset</span><span>(</span><span>$value</span><span>);</span><span></span><span># safety: remove reference</span><span>
  $items</span><span>[]</span><span></span><span>=</span><span> array_map</span><span>(</span><span>'utf8_encode'</span><span>,</span><span> $row </span><span>);</span><span></span><span>}</span></code>
登录后复制

项目中遇到的问题,记录以备后用 .

文献:

json_encode() non utf-8 strings

Olli.ai
Olli.ai

从web或文件数据快速创建数据可视化

Olli.ai 46
查看详情 Olli.ai

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了php json_encode 数据,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号