Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将UTF-8转换为字符串_Java_Utf 8_Character Encoding - Fatal编程技术网

Java 将UTF-8转换为字符串

Java 将UTF-8转换为字符串,java,utf-8,character-encoding,Java,Utf 8,Character Encoding,我从堆栈溢出尝试了很多解决方案,但都不能解决这个问题。 我在UTF-8中有一个JSON对象,其值为title,我需要将其转换为Java字符串: {"id":"118","title":"\u00c7\u00c0\u00c7"} 我最终采用了这种方法,但不起作用: String title = new String(JsonObj.getString("title").getBytes(), "US-ASCII"); String title = new String(JsonObj.get

我从堆栈溢出尝试了很多解决方案,但都不能解决这个问题。 我在UTF-8中有一个JSON对象,其值为
title
,我需要将其转换为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());
英文标题正确显示为沃茨堡、威斯曼、新开。俄罗斯人表现得像Àëòòòò,ÃÀòòòòòò

什么是错误的,如何将其转换为普通字符

编辑:

下面是我如何接收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;
    }
private HashMap getCarBrandsOrModelsFromJson(字符串carBrandsOrModelsJsonStr)抛出JSONException{
//这些是需要提取的JSON对象的名称
最终字符串AA_DATA=“DATA”;
最终字符串AA_TITLE=“TITLE”;
最终字符串AA_ID=“ID”;
JSONObject carBrandsJson=新的JSONObject(carBrandsOrModelsJsonStr);
JSONArray brandsArray=carBrandsJson.getJSONArray(AA_数据);
HashMap carBrandsMap=新HashMap();
对于(int i=0;i>> ?
brand=新字符串(oneBrandJson.getString(AA_TITLE).getBytes(),“UTF8”);
//brand=oneBrandJson.getString(AA_标题);
brand=oneBrandJson.getString(AA_标题);
id=oneBrandJson.getInt(AA_id);
//将品牌和id添加到hashmap中
carBrandsMap.put(品牌、标识);
}
//测井结果
对于(Map.Entry:carBrandsMap.entrySet()){
Log.v(Log_标记,(“\n”+entry.getKey()+”/“+entry.getValue());
}
返回carBrandsMap;
}

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

JSON解析器应该为您解决这个问题<代码>\unnn是JSON规范的一部分(顺便说一句,它不是UTF-8)。这与UTF-8无关。这些是unicode代码点。您使用的解析库是什么(如果它不能处理这类事情,请更改它)@Androider
\unnn
是JSON语法的一部分。JSON解析器将它转换为它使用的任何编码。换句话说,
JsonObj.getString(“title”)
应该为您提供正确的字符串。如果没有,那么你的JSON解析器就坏了。你用什么从Java中读取JSON?你现在得到的结果是什么?如果只使用
JsonObj.getString(“title”)
,那么生成的字符串有多长,它由哪些字符组成?@Androider正确解析JSON字符串
“\u00c7\u00c0\u00c7”
给出字符串
ÀÀĆ
\u00c7
表示U+00C7,拉丁文大写字母C加CEDILLA,等等)。如果这不是您所期望的,那么JSON编码就是错误的。谢谢您的支持。不幸的是,我仍然有这些字符与您的代码。你两次使用UTF-8可以吗?@Androider你到底希望看到什么?我希望看到的是俄语单词,而不是“À,å,À”,Androider在处理后应该是什么样子?现在我有了它,就像是
ÀÀぃ
@Androider是的,没关系,它正在打印Àぃぃぃぃぃぃぃぃ