java解析php函數(shù)json_encode unicode 編碼問題
android開發(fā)中在和服務(wù)器端接口對(duì)接時(shí)出現(xiàn)編碼問題,從服務(wù)器端獲取到的數(shù)據(jù)是 "\u8bbe\u59071ID-\u8bbe\u59071\u540d\u79f0;\u8bbe\u59073id-\u8bbe\u59073\u540d\u79f0;\u8bbe\u59077id-\u8bbe\u59077\u540d\u79f0" 接口是通過php函數(shù)中json_encode進(jìn)行編碼后返回的,在客戶端通過java.net.URLdecoder.decode()解碼不管用,但是直接將以上字符串復(fù)制到decode()方法中可以正常解碼,把接收到的字符串經(jīng)過utf-8編碼后不管用,最后在網(wǎng)上搜索相關(guān)資料找到解決方法。
一,json_encode作用:
json_encode — 對(duì)變量進(jìn)行 JSON 編碼。
說明:string json_encode ($value ),返回 value 值的 JSON 形式。
參數(shù):待編碼的 value ,除了resource 類型之外,可以為任何數(shù)據(jù)類型
該函數(shù)只能接受 UTF-8 編碼的數(shù)據(jù)(譯注:指字符/字符串類型的數(shù)據(jù))
返回值:編碼成功則返回一個(gè)以 JSON 形式表示的 string 。
二,客戶端用java語言解碼:
第一種方法
public String unescapeUnicode(String str){ StringBuffer b=new StringBuffer(); Matcher m = Pattern.compile("\\\\u([0-9a-fA-F]{4})").matcher(str); while(m.find()) b.append((char)Integer.parseInt(m.group(1),16)); return b.toString(); }
直接使用unescapeUnicode()方法解碼就可以了。
2. 使用 json_simple.jar 包解析
下載地址:http://www.dbjr.com.cn/softs/455885.html
JSON.simple是一個(gè)簡(jiǎn)單的Java類庫,用于解析和生成JSON文本。不依賴于其它類庫,性能高。
Object obj=JSONValue.parse(jsonStr);return obj.toString();
PHP服務(wù)器端解決方法:
<html> <head><meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>php生成 json 中文</title> <?php function arrayRecursive(&$array, $function, $apply_to_keys_also = false) { static $recursive_counter = 0; if (++$recursive_counter > 1000) { die('possible deep recursion attack'); } foreach ($array as $key => $value) { if (is_array($value)) { //arrayRecursive($array[$key], $function, $apply_to_keys_also); } else { $array[$key] = $function($value); } if ($apply_to_keys_also && is_string($key)) { $new_key = $function($key); if ($new_key != $key) { $array[$new_key] = $array[$key]; unset($array[$key]); } } } $recursive_counter--; } function JSON($array) { //arrayRecursive($array, 'urlencode', true); //print_r($array); $json = json_encode($array); return urldecode($json); } $array = array ( 'Name'=>urlencode('php生成 json 中文'), 'Age'=>20 ); echo JSON($array); echo '</br>'; echo urlencode('php生成 json 中文'); ?> </body> </html>
- Thinkphp 框架基礎(chǔ)之源碼獲取、環(huán)境要求與目錄結(jié)構(gòu)分析
- php源碼的安裝方法和實(shí)例
- php源碼的使用方法講解
- PHP后臺(tái)備份MySQL數(shù)據(jù)庫的源碼實(shí)例
- PHP自動(dòng)生成縮略圖函數(shù)的源碼示例
- PHP json_encode中文亂碼解決方法
- 基于php解決json_encode中文UNICODE轉(zhuǎn)碼問題
- 淺析PHP中json_encode與json_decode的區(qū)別
- php讓json_encode不自動(dòng)轉(zhuǎn)義斜杠“/”的方法
- 關(guān)于php unset對(duì)json_encode的影響詳解
- PHP中讓json_encode不自動(dòng)轉(zhuǎn)義斜杠“/”的方法
- php json_encode與json_decode詳解及實(shí)例
- php中json_encode不兼容JSON_UNESCAPED_UNICODE的解決方案
- 源碼分析系列之json_encode()如何轉(zhuǎn)化一個(gè)對(duì)象
相關(guān)文章
解決springboot 多線程使用MultipartFile讀取excel文件內(nèi)容報(bào)錯(cuò)問題
這篇文章主要介紹了解決springboot 多線程使用MultipartFile讀取excel文件內(nèi)容報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java 高并發(fā)的三種實(shí)現(xiàn)案例詳解
這篇文章主要介紹了Java 高并發(fā)的三種實(shí)現(xiàn)案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08如何在springboot中配置和使用mybatis-plus
這篇文章主要給大家介紹了關(guān)于如何在springboot中配置和使用mybatis-plus的相關(guān)資料,MyBatis?Plus是MyBatis的增強(qiáng)版,旨在提供更多便捷的特性,減少開發(fā)工作,同時(shí)保留了MyBatis的靈活性和強(qiáng)大性能,需要的朋友可以參考下2023-11-11java獲取新insert數(shù)據(jù)自增id的實(shí)現(xiàn)方法
這篇文章主要介紹了java獲取新insert數(shù)據(jù)自增id的實(shí)現(xiàn)方法,在具體生成id的時(shí)候,我們的操作順序一般是:先在主表中插入記錄,然后獲得自動(dòng)生成的id,以它為基礎(chǔ)插入從表的記錄,需要的朋友可以參考下2019-06-06關(guān)于Mybatis-Plus?Wrapper是否應(yīng)該出現(xiàn)在Servcie類中
最近在做代碼重構(gòu),代碼工程采用了Controller/Service/Dao分層架構(gòu),Dao層使用了Mybatis-Plus框架,本文帶領(lǐng)大家學(xué)習(xí)Mybatis-Plus?Wrapper應(yīng)該出現(xiàn)在Servcie類中嗎,需要的朋友可以參考下2023-05-05