android - 如何从 json 读取\uxxxx

标签 android json unicode

我的 json 格式如下:{"0":{"title":"\u0417\u041d:\u0415\u0432\u0440\u043e\...

如何将\uxxxx编码为可读 View ?

json 我正在采取下一种方式:

HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet("http://android.forum-example.org/?a=1&b=2&c=3");
HttpResponse response = httpClient.execute(request);
resJson = HttpHelper.requestStr(response);
Log.v("JSON:", resJson);

requestStr(response):

public static String requestStr(HttpResponse response){
        String result = "";
        try{
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder str = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null){
                str.append(line + "\n");
            }
            in.close();
            result = str.toString();
        }catch(Exception ex){
            result = "Error";
        }
        return result;
}

即使 result = new String(resJson.getBytes("UTF-8"), "UTF-8"); 也无济于事。但是当我将它与手工字符串一起使用时(例如 String str="\u0435\u043b\u0438\u0415\u0432\"),它就可以工作了。

最佳答案

试试这个,让我知道会发生什么,

HttpResponse response = httpClient.execute(request);

HttpEntity entity = response.getEntity();
// convert entity response to string
  if (entity != null) {
    InputStream is = entity.getContent();
    // convert stream to string
    result = convertStreamToString(is); 
    result = result.replace("\n", "");
    }

这是 convertStreamToString(is);

public static String convertStreamToString(InputStream is) throws Exception {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();
        return sb.toString();
    }

关于android - 如何从 json 读取\uxxxx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8356995/

相关文章:

android - XMPP在android中的实现

json - Elasticsearch 查询的无模式支持

php - Mysql查询WHERE Json value = this

python - 从非 ascii 字符串解码 Python 3 中的转义 unicode

Android Facebook SDK (4.31.0) - CustomTabLoginMethodHandler 中的 ActivityNotFoundException

java - 只打印特定信息到String

android - 如何解决套件构建期间的异常?

java - 使用 Java API 以 JSON 格式获取所有 Cassandra 查询结果

Python、字符串、unicode 字符

python - 循环遍历 unicode 字符串时的奇怪行为