将 json 字符串转换为 UTF-8

Converting json String to UTF-8

我正在用古吉拉特语 json 解析来自 Web 的数据,但是当我在 android 应用程序中收到它时,它看起来像

{"message":"Authorized","u_status":0,"c_Alert":0,"get_data":[{"id":"29","End":"2015-02-19","EntrTime":"2015-02-26","Content":"ભરતીનું સ્થળ - સાબર સ્પોર્ટ્સ સ્ટેડિયમ , હિં&","Begin":"2015-03-10","Header":"લશ્કરી ભરતી મેળો - હિંમતનગર","link":"http:\/\/www.google.com"}],"c_Alert_Msg":"No Message","u_link":"http:\/\/www.google.com","c_Alert_Finish":0,"success":1}

当我从 json 字符串中设置任何过滤文本时,它看起来像

&#274 4;&#276 5;&#272 5;&#273 9;

(我输入 space 因为它在 html 代码中显示完美的 unicode) 但实际上是

"સ્થળ"

我知道那是编码问题,但我如何将这些字符串转换为正确的 unicode 字符

我正在使用以下代码进行 http 请求以获取 json

            try {
                DefaultHttpClient httpclient = new DefaultHttpClient();
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url_for_is);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                String result = EntityUtils.toString(httpResponse.getEntity());
                JSONObject obj = new JSONObject(result);
               Log.d("JSON 123123",obj.toString());

            } catch (JSONException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

我也尝试从 json 获取字符串并将特定字符串转换为 unicode 但没有效果

由此

  JSONObject c = contenTs.getJSONObject(1);
  String headN = c.getString("Header");

  Charset chrutf = Charset.forName("UTF-8");
  final String b = new String(headN.getBytes(),chrutf);
  System.out.println(b);

或者告诉我如何转换像'Ē 4;Ĕ 5;Đ 5;đ 9;'这样的字符到 unicode 字符串

试试这个: String jsonText = EntityUtils.toString(entity, HTTP.UTF_8);

编辑: 可能是这样的:

httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

或者,您是否尝试过使用库 Gson 对您的实体进行编码?

您可以像这样将其包含在您的 build.gradle(模块:app):

dependencies {
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
}

然后使用这部分代码:

httpPost.setEntity(new StringEntity(new Gson().toJson(params), HTTP.UTF_8)));

希望对您有所帮助。

莱昂内尔

我的 post 名声不好,因为他们不知道答案,但我自己找到了解决方案

我只是在代码中将文本转换为html内容并使用

显示
String contentN = json.getJSONArray("get_data").getJSONObject(i).getString("c_Alert_Msg");
Html.fromHtml(contentN))

完整代码

 contenTs = json.getJSONArray("get_data");
 itemList = new ArrayList<HashMap<String, String>>();

 for (int i = 0; i < contenTs.length(); i++) {
            JSONObject c = contenTs.getJSONObject(i);

            // Storing each json item in variable
            String id = c.getString("id");
            String headN = c.getString("Header");
            String contentN = c.getString("Content");
            String time_s = c.getString("Begin");
            String time_e = c.getString("End");
            String linkIn = c.getString("link");

            HashMap map = new HashMap<String, Spannable>();
            String txtHeadN = "<font color=#cc0029><strong>" + String.valueOf(i + 1) + " - " + headN + "</font>";
            map.put("head", Html.fromHtml(txtHeadN));
            map.put("content",Html.fromHtml(contentN));
            map.put("Link", time_s + "  to  " + time_e);
            map.put("links",linkIn);
            // adding HashList to ArrayList
            itemList.add(map);
  }

而且效果很好

// Java will convert it into a UTF-16 representation 
String s = “This is my string” ; 
 
// byte representation in UTF-8 
ByteBuffer byteBuff = StandardCharset.UTF-8.encode(s); 
 
// do what you want with this byte buffer 
String v = new String( bytes, StandardCharset.UTF-8 );