【.net/.net core】使用System.Web.HttpUtility.UrlDecode处理web请求编码问题

问题场景:将数据已JSON字符串写入请求内容后,在接收端接收数据时,数据内容为URL编码后的内容,需要先将接收的字符串数据URL解码后才可转换为数据对象

解决办法:使用System.Web.HttpUtility.UrlDecode(string s)方法对字符串进行URL解码

注:使用System.Net. WebUtility.UrlDecode(string s)解码后,中文内容为unicode编码格式,需要再次将unicode编码转换为中文,经测试System.Web.HttpUtility.UrlDecode(string s)方法可直接转换为中文

代码:

cs 复制代码
//数据类
public class Person{
    public string eid {get;set;}
    public string name {get;set;}
}

//编码字符串数据 
/*
    数据内容为[{"eid":"1214","name":"陈慧敏"},{"eid":"1189","name":"吴莎莎"},{"eid":"1218","name":"陈赟博"}]
*/
string urlCodeStr = "%5b%7b%22eid%22%3a%221214%22%2c%22name%22%3a%22%u9648%u6167%u654f%22%7d%2c%7b%22eid%22%3a%221189%22%2c%22name%22%3a%22%u5434%u838e%u838e%22%7d%2c%7b%22eid%22%3a%221218%22%2c%22name%22%3a%22%u9648%u8d5f%u535a%22%7d%5d"

//使用URL解码方法对字符串进行解码
string jsonStr = System.Web.HttpUtility.UrlDecode(urlCodeStr);

//将解码后的字符串反序列化为List<Person>集合
List<Person> personData = JsonConvert.DeserializeObject<List<Person>>(jsonStr);
相关推荐
csdn_aspnet4 天前
MongoDB C# .NetCore 驱动程序 序列化忽略属性
mongodb·c#·.netcore
Tiger_shl5 天前
【.Net技术栈梳理】08-控制反转(IoC)与依赖注入(DI)
开发语言·.net·.netcore
Tiger_shl5 天前
【.Net技术栈梳理】10-.NET Core 程序的执行
开发语言·.net·.netcore
MoFe15 天前
【.net core】【watercloud】登陆后跳转至指定页面,显示在系统框架页内
.netcore
周杰伦fans5 天前
.net core webapi/mvc阿里云服务器部署 - 错误解决
阿里云·mvc·.netcore
驾驭人生8 天前
Asp .Net Core 系列:Asp .Net Core 集成 Hangfire+MySQL
数据库·mysql·.netcore
时光追逐者8 天前
C#/.NET/.NET Core技术前沿周刊 | 第 53 期(2025年9.1-9.7)
c#·.net·.netcore
somethingGoWay9 天前
wpf .netcore 导出docx文件
wpf·.netcore
somethingGoWay9 天前
wpf .netcore 导出pdf文件
pdf·wpf·.netcore
切糕师学AI10 天前
如何建立针对 .NET Core web 程序的线程池的长期监控
java·前端·.netcore