【.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);
相关推荐
睡前要喝豆奶粉12 小时前
.NET Core Web API开发需引入的三个基本依赖配置说明
oracle·c#·.netcore
睡前要喝豆奶粉15 小时前
.NET Core Web API中数据库相关配置
数据库·c#·.netcore
Archy_Wang_119 小时前
Hangfire 入门与实战:在 .NET Core 中实现可靠后台任务处理
c#·.netcore
睡前要喝豆奶粉2 天前
在.NET Core Web Api中使用redis
redis·c#·.netcore
睡前要喝豆奶粉3 天前
多表分页联查——EF Core方式和Dapper方式
c#·.netcore
csdn_aspnet3 天前
.NETCore、.NET 7 和 RabbitMQ 的发布-订阅模式
rabbitmq·.netcore·.net7.
爱吃香蕉的阿豪3 天前
深入理解 .NET Core 中的 IServiceScopeFactory:用法、场景与静态类依赖注入
.netcore
sky-stars4 天前
.NET 泛型编程(泛型类、泛型方法、泛型接口、泛型委托、泛型约束)
c#·.net·.netcore
The Sheep 20234 天前
.NetCoreMVC 开发网页使用sass
.netcore·sass
宝桥南山5 天前
.NET10 - 尝试一下Blazor Web Assembly Standalone App的fingerprint新特性
microsoft·微软·c#·asp.net·.net·.netcore