【问题标题】:how to remove � in json string android?如何在 json 字符串 android 中删除 �?
【发布时间】:2015-04-29 03:14:07
【问题描述】:

每当我解析JSON字符串并在android Logcat中显示json输出的输出时,我总是发现

"f������a������m������i������l������y������.��� ���p������n������g"

在 Logcat 中。实际的字符串是“family.png”,但显示如上。知道如何解决这个问题吗?

这是 JSON 数据。

{"members": [
   {"user":"���d���e���8���8���f���5���c���7���3���7���1���4���7���6���6���f", "username":"���P���P���S���h���e���i���n", "avatar":"f������a������m������i������l������y������.������p������n������g"} 
    ]}

并由 Coldfusion 生成 JSON 格式。

{"members": [
<cfoutput query="getQry">
{"user":"#tuser#", "username":"#tusername#", "avatar":"#tpicture#"} <cfif currentrow LT getTrackQry.recordcount>,</cfif>
</cfoutput>
]}

这里是从 URL 解析 JSON 对象。

public static JSONObject getFromUrl(String url) {
        InputStream is = null;
        String content = null;
        JSONObject jArray = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(new HttpGet(url));
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }
        catch(Exception e){
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        try{
              BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf8"),8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              content = sb.toString();
        } catch(Exception e){
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {
            jArray = new JSONObject(content);
        } catch (JSONException e){
            Log.e("log_tag", "Error parsing data " + e.toString());
        }           

        return jArray;      
    }

【问题讨论】:

  • 你控制 JSON 数据吗?你自己生成吗?显示使用代码。向我们展示 JSON。
  • 是的,我自己生成了 JSON 数据并在我的问题中添加了 JSON 数据。
  • @Pang 我知道。这就是为什么我要问为什么“family.png”会错成“f������a������m������i������l������ android logcat 中的“y������.������p������n������g”。
  • @ppshein 好的,我们很接近了。贴出你用来解析它的代码。
  • new BufferedReader(new InputStreamReader(is,"utf8"),8) 应该是new BufferedReader(new InputStreamReader(is, "UTF-8")),请参阅stackoverflow.com/questions/6511880/…

标签: android json


【解决方案1】:

变化:

new BufferedReader(new InputStreamReader(is,"utf8"),8)

收件人:

new BufferedReader(new InputStreamReader(is, "UTF-8"))

参考资料:

请参阅另一个 SO 问题:How to parse a JSON Input stream

public InputStreamReader(InputStream in, CharsetDecoder dec) 的官方文档是here

UTF-8 字符集信息也位于here

【讨论】:

    【解决方案2】:

    您似乎使用了不正确的字符集。 您是否尝试过从以下位置更改此行:

    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf8"),8);
    

    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
    

    【讨论】:

    • 我碰巧在这里查看我的代码并注意到使用的字符集是罪魁祸首。没有看cmets。太多了。
    猜你喜欢
    • 1970-01-01
    • 2015-12-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    相关资源
    最近更新 更多