项目场景:
c#程序得到json数据中中文部分显示为乱码
问题描述
用这个函数转换
public static string HexUnicodeToStr(string strDecode)
{
string[] strDecode2;
string strDecode3 = "";
strDecode2 = strDecode.Split('\\');
if (strDecode2.Length > 0)
{
for (int i = 1; i < strDecode2.Length; i++)
{
string str = strDecode2[i].Substring(1, 4);
try
{
strDecode3 = (char)int.Parse(str, System.Globalization.NumberStyles.HexNumber) + "";
}
catch (Exception ex)
{
// MessageBox.Show(ex.ToString(), i.ToString());
}
strDecode = strDecode.Replace("\\u" + str, strDecode3);
}
}
return strDecode; ;
}
结果输出:
进入:{“cityid”:“101200101”,“date”:“2022-04-13”,“week”:“\u661f\u671f\u4e09”,“update_time”:“10:14”,“city”:“\u6b66\u6c49”,“cityEn”:“wuhan”,“country”:“\u4e2d\u56fd”,“countryEn”:“China”,“wea”:“\u591a\u4e91”,“wea_img”:“yun”,“tem”:“14”,“tem1”:“18”,“tem2”:“11”,“win”:“\u5357\u98ce”,“win_speed”:“3\u7ea7”,“win_meter”:“15km/h”,“humidity”:“97%”,“visibility”:“6km”,“pressure”:“1007”,“air”:“19”,“air_pm25”:“12”,“air_level”:“\u4f18”,“air_tips”:“\u7a7a\u6c14\u5f88\u597d\uff0c\u53ef\u4ee5\u5916\u51fa\u6d3b\u52a8\uff0c\u547c\u5438\u65b0\u9c9c\u7a7a\u6c14\uff0c\u62e5\u62b1\u5927\u81ea\u7136\uff01”,“alarm”:{“alarm_type”:“”,“alarm_level”:“”,“alarm_content”:“”},“win_speed_day”:“4-5\u7ea7”,“win_speed_night”:“3-4\u7ea7”,“aqi”:{“update_time”:“09:17”,“cityid”:“101200101”,“city”:“\u6b66\u6c49”,“cityEn”:“wuhan”,“country”:“\u4e2d\u56fd”,“countryEn”:“China”,“air”:“19”,“air_level”:“\u4f18”,“air_tips”:“\u7a7a\u6c14\u5f88\u597d\uff0c\u53ef\u4ee5\u5916\u51fa\u6d3b\u52a8\uff0c\u547c\u5438\u65b0\u9c9c\u7a7a\u6c14\uff0c\u62e5\u62b1\u5927\u81ea\u7136\uff01”,“pm25”:“12”,“pm25_desc”:“\u4f18”,“pm10”:“17”,“pm10_desc”:“\u4f18”,“o3”:“62”,“o3_desc”:“\u4f18”,“no2”:“15”,“no2_desc”:“\u4f18”,“so2”:“5”,“so2_desc”:“\u4f18”,“co”:“-”,“co_desc”:“-”,“kouzhao”:“\u4e0d\u7528\u4f69\u6234\u53e3\u7f69”,“yundong”:“\u975e\u5e38\u9002\u5b9c\u8fd0\u52a8”,“waichu”:“\u9002\u5b9c\u5916\u51fa”,“kaichuang”:“\u9002\u5b9c\u5f00\u7a97”,“jinghuaqi”:“\u5173\u95ed\u51c0\u5316\u5668”}}
输出:{“cityid”:“101200101”,“date”:“2022-04-13”,“week”:“星期三”,“update_time”:“10:14”,“city”:“武汉”,“cityEn”:“wuhan”,“country”:“中国”,“countryEn”:“China”,“wea”:“多云”,“wea_img”:“yun”,“tem”:“14”,“tem1”:“18”,“tem2”:“11”,“win”:“南风”,“win_speed”:“3级”,“win_meter”:“15km/h”,“humidity”:“97%”,“visibility”:“6km”,“pressure”:“1007”,“air”:“19”,“air_pm25”:“12”,“air_level”:“优”,“air_tips”:“空气很好,可以外出活动,呼吸新鲜空气,拥抱大自然!”,“alarm”:{“alarm_type”:“”,“alarm_level”:“”,“alarm_content”:“”},“win_speed_day”:“4-5级”,“win_speed_night”:“3-4级”,“aqi”:{“update_time”:“09:17”,“cityid”:“101200101”,“city”:“武汉”,“cityEn”:“wuhan”,“country”:“中国”,“countryEn”:“China”,“air”:“19”,“air_level”:“优”,“air_tips”:“空气很好,可以外出活动,呼吸新鲜空气,拥抱大自然!”,“pm25”:“12”,“pm25_desc”:“优”,“pm10”:“17”,“pm10_desc”:“优”,“o3”:“62”,“o3_desc”:“优”,“no2”:“15”,“no2_desc”:“优”,“so2”:“5”,“so2_desc”:“优”,“co”:“-”,“co_desc”:“-”,“kouzhao”:“不用佩戴口罩”,“yundong”:“非常适宜运动”,“waichu”:“适宜外出”,“kaichuang”:“适宜开窗”,“jinghuaqi”:“关闭净化器”}}