将UTF-8转换为字符串

3

我尝试了很多来自Stack Overflow的解决方案,但无法解决问题。 我有一个包含值为UTF-8的title的JSON对象,并且需要将其转换为Java字符串:

{"id":"118","title":"\u00c7\u00c0\u00c7"}

我使���了这种方法,但它没有起作用:
String title = new String(JsonObj.getString("title").getBytes(), "US-ASCII"); 

String title = new String(JsonObj.getString("title").getBytes());

英文标题以Wartburg,Wiesmann,Xin Kai的形式正确显示。俄语标题则像ÂÀÇ,Âåëòà,ÃÀÇ一样显示。

问题出在哪里,如何将其转换为正常字符?

编辑:

以下是我接收JSON的方式

 JSONObject jsonObject = new JSONObject();

            try {

                //                sending empty JSON in this request
                String jsonRequest = jsonObject.toString();
                Log.v(LOG_TAG, "JSON: " + jsonRequest);

                URL url = new URL(STRING_URL);

                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("POST");

                //  hashing the signature
                String md5Signature = MD5Utils.md5Apache(KEY + jsonRequest);

                //                setting heading property
                urlConnection.setRequestProperty(AA_SIGNATURE, md5Signature);

                urlConnection.setDoOutput(true);
                DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
                wr.writeBytes(jsonRequest);
                wr.flush();
                wr.close();

                //            read the inputshtream into the String
                InputStream inputStream = urlConnection.getInputStream();

                if (inputStream == null) {
                    //                nothing to do
                    return null;
                }

                reader = new BufferedReader(
                        new InputStreamReader(inputStream));

                String inputLine;
                StringBuffer buffer = new StringBuffer();

                while ((inputLine = reader.readLine()) != null) {
                    buffer.append(inputLine);
                }

                if (buffer.length() == 0) {
                    // Stream was empty
                    return null;
                }

                // String buffer
                String responseJsonStr = buffer.toString();
                Log.v(LOG_TAG, "Final String buffer: " + responseJsonStr);


                //                trying to parse json string and return result
                try {
                    return getCarBrandsOrModelsFromJson(responseJsonStr);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {

                if (urlConnection != null) {
                    urlConnection.disconnect();
                }

                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        Log.e(LOG_TAG, "Error closing stream");
                    }
                }
            }
            return null;
        }

这是我进行解析的方法

 private HashMap<String, Integer> getCarBrandsOrModelsFromJson(String carBrandsOrModelsJsonStr) throws JSONException {

        //        these are the names of JSON objects needed to be extracted
        final String AA_DATA = "data";
        final String AA_TITLE = "title";
        final String AA_ID = "id";

        JSONObject carBrandsJson = new JSONObject(carBrandsOrModelsJsonStr);
        JSONArray brandsArray = carBrandsJson.getJSONArray(AA_DATA);

        HashMap<String, Integer> carBrandsMap = new HashMap<String, Integer>();

        for (int i = 0; i < brandsArray.length(); i++) {

            String brand = null;
            Integer id;

            //            Get the JSON object representing the one brand
            JSONObject oneBrandJson = brandsArray.getJSONObject(i);

            //            getting brand and id

            // ===================>>> ?
            brand = new String(oneBrandJson.getString(AA_TITLE).getBytes(), "UTF8");
            //            brand = oneBrandJson.getString(AA_TITLE);
            brand = oneBrandJson.getString(AA_TITLE);

            id = oneBrandJson.getInt(AA_ID);

            //            adding brand and id into hashmap
            carBrandsMap.put(brand, id);
        }

        //        Logging result
        for (Map.Entry<String, Integer> entry : carBrandsMap.entrySet()) {
            Log.v(LOG_TAG, ("\n" + entry.getKey() + " / " + entry.getValue()));
        }

        return carBrandsMap;
    }

4
你的JSON解析器应该会自动处理这个问题。\unnnn是JSON规范的一部分(顺便提一下,它不是UTF-8编码)。 - Biffen
1
这与 utf-8 无关,而是 Unicode 代码点。你正在使用什么解析库(如果它不处理这种事情的话,那就换一个吧)。 - njzk2
1
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Biffen
1
如果您需要俄语单词,那么它们可能是“ВАЗ”、“Велта”、“ГАЗ”。正确的编码是“\u0412\u0410\u0417”、“\u0412\u0435\u043B\u0442\u0430”、“\u0413\u0410\u0417”。正如@Biffen所建议的那样,字符串在另一端已损坏。看看会发生什么:使用俄语“Windows-1251” 8位编码存储了一个俄语字符“Г”,其值为195或\xC3;然后,错误地将此值用作Unicode值\u00C3,而实际上\u00C3是扩展拉丁字符Ã。正确的方式是使用实际的Unicode值\u0413来表示Г。 - Goblin Alchemist
1
严格来说,您的输入JSON数据已经损坏。如果JSON是由请求生成的,并且您可以控制生成它的模块,则最好修复该模块以在JSON中具有正确的Unicode。否则,如果您无法控制系统的那部分,或者如果您有数百万个损坏的JSON文件的数据库,并且您无法重新生成它们所有,则必须尝试在您这一侧“解码”损坏的数据... 您将不得不知道文本实际上是俄语(现在这些信息已经丢失了),并且它将无法用于法语或中文。 - Goblin Alchemist
显示剩余11条评论
1个回答

2
以下代码将Unicode转换为UTF-8。
String original = JsonObj.getString("title");
try {
   byte[] utf8Bytes = original.getBytes("UTF-8");
   String roundTrip = new String(utf8Bytes, "UTF-8");
} 
catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

编辑:

看起来您的Unicode字符串之前已经被编码为cp1252。要解码回去,您应该使用

String roundTrip = new String(utf8Bytes);
byte[] bytes= roundTrip.getBytes("cp1252");
String roundTrip2 = new String(bytes, "cp1251");

谢谢。不幸的是,我仍然在您的代码中遇到这些字符。您使用UTF-8两次是否可以? - Androider
@Androider,\u00c7\u00c0\u00c7 处理后应该是什么样子?目前我处理后得到的是 ÇÀÇ - catch23
@Androider,没问题,它正在打印 ÇÀÇ。 - Roman C
我在另一个论坛上得到了一个解码器的建议:https://www.artlebedev.ru/tools/decoder/ 它展示了文本ÂÀÇ,Âåëòà,ÃÀÇ应该是这样的:ВАЗ,Велта,ГАЗ。它表明以这种方式解码了文本:CP1252 → CP1251。所以,文本可能是使用CP1252解码的?我尝试在您的代码中将“UTF-8”更改为“windows-1252”,但我仍然看不到正确的字符。 - Androider
谢谢!您的代码完美运行。顺便说一句,дякую, земляче:) - Androider
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接