我用古吉拉特语的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字符串设置任何过滤后的文本时,它看起来像
Ē 4;Ĕ 5;Đ 5;đ 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 string
答案 0 :(得分:1)
编辑: 也许是这样的:
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
或者,您是否尝试使用库Gson编码实体?
您可以在 build.gradle(模块:应用)中包含这样的内容:
dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
}
然后使用这部分代码:
httpPost.setEntity(new StringEntity(new Gson().toJson(params), HTTP.UTF_8)));
希望它有所帮助。
梅西
答案 1 :(得分: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);
}
它完美无缺
答案 2 :(得分:0)
试试这个:
String jsonText = EntityUtils.toString(entity, HTTP.UTF_8);
答案 3 :(得分:0)
// 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 );