As i'm trying to access object values using JsonSerializer.Deserialize using debugger.
Here is my result which i'm having below.
OtpData = ValueKind = Object : "{
"OTP":"3245234",
"UserName":"mohit840",
"type":"SuperAdmin"
}"
As i try to access it using var Data = JsonSerializer.Deserialize(OtpData)
it gives me following error below.
How can i access the inside and get values of the following object.
"OTP":"3245234",
"UserName":"mohit840",
"type":"SuperAdmin"
Update :
[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 });
}
Update 2: As i'm posting this by Postman application.
{
"OTP":"3245234",
"UserName":"mohit840",
"type":"SuperAdmin"
}
dynamic
if you know it's a string and it only works if it's a string? You shouldn't be using dynamic typing unless you actually need it. Not having to deal with problems like this is one of the many reasons why. – Graehldynamic OptData
, returned byOptData.GetType()
? It's probably not any of the types accepted by any of the overloads toJsonSerializer.Deserialize()
. But since you're usingdynamic
this sort of error can only be caught in runtime not compile time. – Acrossdynamic
. That exception is specific to dynamically compiling code. If the types of your code aren't valid, they'll be compiler errors instead. – GraehlOtpData
? You've tagged this with both json.net and system.text.json so we really have no way of guessing what that really is. I mean specifically what is returned if you examine the value ofOtpData.GetType().FullName
in the debugger. – Acrossdynamic
doesn't change that at all. The documentation of whatever deserialier you're using will cover how to inspect objects that don't have a static structure, but that's based on the output, not the input. – Graehl