首頁 後端開發 PHP問題 PHP中json_decode函數中文亂碼如何解決?

PHP中json_decode函數中文亂碼如何解決?

Jul 21, 2020 pm 04:00 PM
php

PHP中json_decode函數中文亂碼如何解決?

PHP中json_decode函數中文亂碼如何解決?

PHP中json_decode函數中文亂碼解決方法:1、使用函數「urldecode()」將資料解碼,解碼後再進行JSON解碼,其函數的作用是解碼已編碼的URL字串;2 、在JSON編碼的時候,不要將中文編碼即可。

範例程式碼

<?php
    $testJSON=array(&#39;name&#39;=>&#39;中文字符串&#39;,&#39;value&#39;=>&#39;test&#39;);
    echo json_encode($testJSON);
?>

查看输出结果为:
{“name”:”\u4e2d\u6587\u5b57\u7b26\u4e32″,”value”:”test”}
可见即使用UTF8编码的字符,使用json_encode也出现了中文乱码。解决办法是在使用json_encode之前把字符用函数urlencode()处理一下,然后再json_encode,输出结果的时候在用函数urldecode()转回来。具体如下:


<?php
    $testJSON=array(&#39;name&#39;=>&#39;中文字符串&#39;,&#39;value&#39;=>&#39;test&#39;);
    //echo json_encode($testJSON);
    foreach ( $testJSON as $key => $value ) {
        $testJSON[$key] = urlencode ( $value );
    }
    echo urldecode ( json_encode ( $testJSON ) );
?>


查看输出结果为:



{“name”:”中文字符串”,”value”:”test”}

推薦教學:《PHP

以上是PHP中json_decode函數中文亂碼如何解決?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Stock Market GPT

Stock Market GPT

人工智慧支援投資研究,做出更明智的決策

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

如何檢查電子郵件地址在PHP中是否有效? 如何檢查電子郵件地址在PHP中是否有效? Sep 21, 2025 am 04:07 AM

usefilter_var()

KOOK語音平台官網 _ PC客戶端及網頁版登錄入口 KOOK語音平台官網 _ PC客戶端及網頁版登錄入口 Sep 20, 2025 am 11:30 AM

KOOK語音平台官網為https://www.kookapp.cn/,支持PC客戶端及網頁版登錄,提供Windows、Android、iOS多端覆蓋,用戶可下載客戶端或通過瀏覽器直接訪問使用。

如何在PHP中製作對象的深度副本或克隆? 如何在PHP中製作對象的深度副本或克隆? Sep 21, 2025 am 12:30 AM

useunSerialize(serialize($ obj))fordeepcopyingwhenalldataiSerializable;否則,exhiment__clone()tomanallyDuplicateNestedObjectedObjectSandAvoidSharedReference。

如何合併PHP中的兩個陣列? 如何合併PHP中的兩個陣列? Sep 21, 2025 am 12:26 AM

usearray_merge()tocombinearrays,oftritingDupritingDuplicateStringKeySandReIndexingNumericKeys; forsimplerconcatenation,尤其是innphp5.6,usethesplatoperator [... $ array1,... $ array2]。

如何在PHP項目中使用名稱空間? 如何在PHP項目中使用名稱空間? Sep 21, 2025 am 01:28 AM

NamespacesinPHPorganizecodeandpreventnamingconflictsbygroupingclasses,interfaces,functions,andconstantsunderaspecificname.2.Defineanamespaceusingthenamespacekeywordatthetopofafile,followedbythenamespacename,suchasApp\Controllers.3.Usetheusekeywordtoi

如何在PHP中調用父構建器? 如何在PHP中調用父構建器? Sep 21, 2025 am 03:43 AM

當extervendingAclassInphp時,callparent :: __ construct()toensuretheparent'sinitializationLogicRuns.1.iftheparentClassConstructorSetSupessentialProperties,SkeippingThiscallMayResultInCompleteTeleteTeTeborteTepleteTebignEbigntInitialization.2.useparent.2.useceparent.2.usectectectectecon :: __ _______________________________)insideTheChild’sco

如何使用PHP更新數據庫中的記錄? 如何使用PHP更新數據庫中的記錄? Sep 21, 2025 am 04:47 AM

toupdateadatabaseRecordInphp,firstConnectusingpDoormySqli,thenusepreparedStatementStoExecuteAsecuteAsecuresqurupDatequery.example.example:$ pdo = newpdo(“ mySql:mysql:host = localHost; localhost; localhost; dbname; dbname = your_database = your_database',yous_database',$ username,$ username,$ squeaste;

如何查看PHP中的文件權限? 如何查看PHP中的文件權限? Sep 22, 2025 am 06:27 AM

usefileperms()togetFilePermissionsasanIntegerAntegatusingsPrintf('%o')

See all articles