Android 将json字符串转换为UTF-8

Android 将json字符串转换为UTF-8,android,json,unicode,utf-8,Android,Json,Unicode,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":"ભરતીનુ&#

我正在用古吉拉特语用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}
&#274 4;&#276 5;&#272 5;&#273 9;
当我从json字符串中设置任何过滤文本时

{"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}
&#274 4;&#276 5;&#272 5;&#273 9;
(我放了空格,因为它在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);
或者告诉我如何将“Ē;Ĕ 5;Đ 5;đ 9;”等字符转换为unicode字符串
String jsonText=EntityUtils.toString(entity,HTTP.UTF_8);
编辑: 也许是这样:

httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
或者,您是否尝试过使用库Gson对实体进行编码

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

然后使用这部分代码:

httpPost.setEntity(new StringEntity(new Gson().toJson(params), HTTP.UTF_8)));
希望能有帮助


莱昂内尔(Lionel)

我的帖子给人负面的印象真的很糟糕,因为他们不知道答案,但我自己找到了解决办法

我只是在代码中将文本转换为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);
  }
contenTs=json.getJSONArray(“获取数据”);
itemList=新的ArrayList();
对于(int i=0;i”+String.valueOf(i+1)+-“+headN+”;
map.put(“head”,Html.fromHtml(txtHeadN));
map.put(“content”,Html.fromHtml(contentN));
映射放置(“链接”,时间+到“+时间”;
地图放置(“链接”,链接);
//将哈希列表添加到ArrayList
itemList.add(map);
}

它工作得非常好

在将结果插入JSONObject之前是否尝试转换结果?是否表示要转换什么?
String result=EntityUtils.toString(httpResponse.getEntity(),HTTP.UTF_8);JSONObject obj=new JSONObject(result);
@3d0无效字符串仍然有效was@3d0,非常感谢,您的解决方案帮助了我!不,我已经看到gson被弃用了,我对它不太了解,所以我只使用json响应的个性化代码。我编辑了我的帖子:使用新的StringEntity(新的UrlEncodedFormEntity(params),HTTP.UTF_8)也许?新的StringEntity(新的UrlEncodedFormEntity(params)不起作用,因为“org.apache.http.entity.StringEntity”中的“StringEntity(java.lang.String,java.lang.String)”不能应用于“(org.apache.http.client.entity.urlencodedformity,java.lang.String)”,我想我已经找到了:httpPost.setEntity(新的urlencodedformity(params,http.utf8));不,我不知道是什么问题,我也使用了你的代码,但仍然没有效果
// 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 );