c# - 动态 c# ValueKind = Object
问题描述
当我尝试使用JsonSerializer.Deserialize 使用调试器访问对象值时。
这是我在下面的结果。
OtpData = ValueKind = Object : "{
"OTP":"3245234",
"UserName":"mohit840",
"type":"SuperAdmin"
}"
当我尝试使用var Data = JsonSerializer.Deserialize(OtpData)
它访问它时,我在下面给出了以下错误。
我如何访问内部并获取以下对象的值。
"OTP":"3245234",
"UserName":"mohit840",
"type":"SuperAdmin"
更新 :
[AllowAnonymous]
[HttpPost("ValidateOTP")]
public IActionResult ValidOTP(dynamic OtpData)
{
bool Result = false;
var Data = JsonSerializer.Deserialize(OtpData);
if (OtpData.type == "SuperAdmin")
{
Users _Users = _Context.Users.FirstOrDefault(j => j.Username == "");
if (_Users != null)
{
_Users.OTP = OtpData.OTP;
_Users.VerififedOTP = true;
_Context.SaveChanges();
Result = true;
}
}
else
{
Staff _Staff = _Context.Staffs.FirstOrDefault(j => j.Username == "");
if (_Staff != null)
{
_Staff.OTP = OtpData.OTP;
_Staff.VerififedOTP = true;
_Context.SaveChanges();
Result = true;
}
}
return Ok(new { Result = Result });
}
更新 2: 当我通过邮递员应用程序发布此内容时。
{
"OTP":"3245234",
"UserName":"mohit840",
"type":"SuperAdmin"
}
解决方案
从 Asp.net Core 3.0 的新版本开始,Microsoft 附带了 System.Text.Json,它引入了新的JsonElement
ValueKind
so ValueKind
is from System.Text.Json
library 和JsonConvert
is from Newtonsoft
library。
解析度 :-
Newtonsoft.Json
您可能在序列化您可能使用过的对象和反序列化您可能使用过的对象时混合了两个库,System.Text.Json
反之亦然。
推荐阅读
- android - 应用链接在三星和小米手机上不起作用
- c - 尽管使用了 fclose(),文件并没有被关闭,因此我不能使用 remove() 函数
- c# - Unity 3d 场景正在加载/未加载
- python - 在网页上托管 Jupyter Notebook Python 应用程序
- python - 有没有办法抓取使用 python 加载的数据
- python - Django - 带有 2 个参数的 URL 模板标签
- python - 如何将数据帧转换为调用api的字典?
- c# - Rhino 嘲笑 AssertWasCalled 不起作用
- ios - react-native InAppUtils.receiptData 的问题
- arrays - 在 Swift 4 中将 Json 对象转换为数组格式