android开发中在和服务器端接口对接时出现编码问题,从服务器端获取到的数据是 "\u8bbe\u59071ID-\u8bbe\u59071\u540d\u79f0;\u8bbe\u59073id-\u8bbe\u59073\u540d\u79f0;\u8bbe\u59077id-\u8bbe\u59077\u540d\u79f0" 接口是经过php函数中json_encode进行编码后返回的,在客户端经过java.net.URLdecoder.decode()解码无论用,可是直接将以上字符串复制到decode()方法中能够正常解码,把接收到的字符串通过utf-8编码后无论用,最后在网上搜索相关资料找到解决方法。php
一,json_encode做用:java
说明:string json_encode ($value ),返回 value 值的 JSON 形式。android
参数:待编码的 value ,除了resource 类型以外,能够为任何数据类型json
该函数只能接受 UTF-8 编码的数据(译注:指字符/字符串类型的数据)服务器
返回值:编码成功则返回一个以 JSON 形式表示的 string 。app
二,客户端用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 包解析google
下载地址:http://code.google.com/p/json-simple/downloads/list编码
JSON.simple是一个简单的Java类库,用于解析和生成JSON文本。不依赖于其它类库,性能高。
Object obj=JSONValue.parse(jsonStr); return obj.toString();