如何从json读取\ uxxxx

时间:2011-12-02 13:26:45

标签: android json unicode

我的 json 格式如下: {“0”:{“title”:“\ u0417 \ u041d:\ u0415 \ u0432 \ u0440 \ u043e \ ... < / em>的

如何将 \ uxxxx 编码为可读取的视图?

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(响应)

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 \”)时,它就可以了。

1 个答案:

答案 0 :(得分:1)

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

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();
    }