将json String转换为UTF-8

问题描述 投票:0回答:3

我正在使用gujarati语言在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;

(我推荐空间因为它在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);

或告诉我我可以转换字符的方式,如'&#274 4;&#276 5;&#272 5;&#273 9;' unicode字符串

android json unicode utf-8
3个回答
1
投票

由于他们不知道答案但我自己找到了解决方案,因此我的帖子给出负面声誉真的很糟糕

我只需将代码中的文本转换为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);
  }

它工作得非常好


1
投票

编辑:也许是这样的:

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

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

你可以在build.gradle(Module: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)));

希望能帮助到你。

莱昂内尔


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.