使用 Newtonsoft.Json 将表单数据转换成对象

对于内容较多的表单一个个获取赋值比较麻烦,通过对象转换几行代码就可以完成表单数据读取到对象

先将表单数据转换成json

cs 复制代码
string json = JsonConvert.SerializeObject(Request.Form);

转换后的json

javascript 复制代码
{
    "id":"1234",
    "name":"username",
    "address":"address"
}

如果表单提交名称和对象内不一致也可以通过配置 Newtonsoft.Json 进行转换,不支持的格式也可以通过自定义转换处理

再将json数据转成对象

cs 复制代码
class userInfo{

    public int id{get;set;}

    public string name{get;set;}

    public string address{get;set;}
}

userInfo objUserInfo = JsonConvert.DeserializeObject<userInfo>(json);
相关推荐
Irene199117 小时前
Prettier 配置文件 .prettierrc.js 和 .prettierrc.json 的区别
javascript·json
23G1 天前
Fastjson转换jsonStr时设置SerializerFeature.DisableCircularReferenceDetect不生效
json
函数的彼端1 天前
iOS Model Generator - 让 JSON 转模型变得简单高效
ios·json·cocoa
吕吕-lvlv1 天前
实现轻松存取JSON的小工具
json
web前端进阶者1 天前
electron-vite报错Unexpected end of JSON input
javascript·electron·json
亚林瓜子1 天前
在AWS Athena中使用json_extract_scalar函数对某个json字段进行过滤和分组统计
sql·json·aws·athena
micro_cloud_fly1 天前
langchain langgraph历史会话的 json序列化
python·langchain·json
doupoa1 天前
VitePressv2.0 + TailwindCSSv4.1 集成方案
typescript·前端框架·json·html5·postcss
时光轻浅,半夏挽歌2 天前
python不同格式文件的读写方式(json等)
python·json
郭庆汝2 天前
json库保存字典数据到本地
人工智能·深度学习·json