将UTF-8转换为字符串

安卓人

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

{"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;
    }
罗马C

下面的代码从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");

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将xml字符串UTF8转换为UTF16?

来自分类Dev

如何将xml字符串UTF8转换为UTF16?

来自分类Dev

Javascript将字符串从utf-8转换为iso-8859-1

来自分类Dev

将文本utf8转换为char或字符串

来自分类Dev

在Ruby中将字符串从UTF-8转换为ASCII-8BIT?

来自分类Dev

将字符串转换为UTF 8

来自分类Dev

将字符串转换为UTF8

来自分类Dev

将byte []转换为UTF8的字符串

来自分类Dev

使用Dart语言将字符串从ISO-8859-2转换为UTF-8

来自分类Dev

使用Dart语言将字符串从ISO-8859-2转换为UTF-8

来自分类Dev

Ruby将字符串编码从ISO-8859-1转换为UTF-8无效

来自分类Dev

在Python中将UTF-8转换为字符串文字

来自分类Dev

如何在C#中将UTF-8转换为字符串

来自分类Dev

如何将uint8转换为字符串

来自分类Dev

如何将[] int8转换为字符串

来自分类Dev

字符串UTF-8转换问题

来自分类Dev

将URL编码的字符串(UTF-8)转换为Python中的字符串?

来自分类Dev

Python:将utf-8字符串转换为字节字符串

来自分类Dev

Python:将utf-8字符串转换为字节字符串

来自分类Dev

将UTF-8字符串转换为可读字符串

来自分类Dev

Delphi-将字符串从UTF-8转换回

来自分类Dev

修改字符串以将1转换为01

来自分类Dev

如何将字符串\\ u6d4e \\ u5357 \\ u5e02转换为utf-8数据

来自分类Dev

如何将日期时间字符串中的Java 8转换为长(UNIX大纪元)(斯卡拉)

来自分类Dev

将“ =?UTF 8?..”(RFC 2047)转换为golang中的常规字符串

来自分类Dev

将字节缓冲区转换为UTF8字符串

来自分类Dev

将流(utf8)数据转换为字符串的安全方法是什么?

来自分类Dev

如何使用iconv(3)将宽字符串转换为UTF-8?

来自分类Dev

将包含utf-8符号的json字符串转换为php数组

Related 相关文章

热门标签

归档