首页 > 解决方案 > 动态 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"
}

标签: c#asp.net-coresystem.text.json

解决方案


从 Asp.net Core 3.0 的新版本开始,Microsoft 附带了 System.Text.Json,它引入了新的JsonElement ValueKindso ValueKindis from System.Text.Jsonlibrary 和JsonConvertis from Newtonsoftlibrary。

解析度 :-

Newtonsoft.Json您可能在序列化您可能使用过的对象和反序列化您可能使用过的对象时混合了两个库,System.Text.Json反之亦然。


推荐阅读