Home php教程 php手册 疑难答疑 PHP服务端返回Json问题

疑难答疑 PHP服务端返回Json问题

Jun 13, 2016 am 11:10 AM
json php Ideas us Serve flexible Difficulties of end Q&A experience programming return question

我们需要在不断的经验积累中去完善我们的编程思路,灵活运用

后来看json_encode的文档才知道,它只能编码UTF-8的字符。这个问题就是返回的字符是GB2312,怎么变为UTF-8,并且最好是将整个数组进行UTF-8的转换。经过搜索有以下代码解决PHP服务端返回Json问题:

  1. function encodeUTF8($array)   
  2. {   
  3. foreach($array as $key=>$value)   
  4. {   
  5. if(!is_array($value))   
  6. {   
  7. $array[$key]=mb_convert
    _encoding($value,"UTF-8","GBK");   
  8. }   
  9. else   
  10. {   
  11. encodeUTF8($array[$key]);   
  12. }   
  13. }   
  14. return $array;   
  15. }  

每次返回结果时:

<ol class="dp-xml"><li class="alt"><span><span>echo '{"total":"'.$rscount.'<br>","results":'.json_encode<br>(encodeUTF8($result)).'}'; </span></span></li></ol>
Copy after login

以上这段代码就是PHP服务端返回Json问题的具体解决方法。


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 Article Tags

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

PHP Program to Count Vowels in a String

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

The Key to Coding: Unlocking the Power of Python for Beginners

Java Made Simple: A Beginner's Guide to Programming Power Java Made Simple: A Beginner's Guide to Programming Power Oct 11, 2024 pm 06:30 PM

Java Made Simple: A Beginner's Guide to Programming Power

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Create the Future: Java Programming for Absolute Beginners

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder

See all articles