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"
}
From new release of Asp.net Core 3.0 Microsoft has come with System.Text.Json which introduce new JsonElement
ValueKind
so ValueKind
is from System.Text.Json
library and JsonConvert
is from Newtonsoft
library.
Resolution :-
it is possible that you have mixed both the library while Serializing the object you may have used Newtonsoft.Json
and while Deserilizing you may have used System.Text.Json
or vice versa.
Found a real way to tackle it i have used JObject
instead of dynamic
. Here is a sample below.
public ResponseWrapper<GenericResponseModel> PostServiceStreams(JObject jObject)
{
return ResponseService.ReturnResponse(() =>
{
return new GenericResponseModel(true, string.Empty,database.MarkedIssues.Select(j => j).ToList().Select(i => i.MapMarkedIssuesModels()).ToList());
}, Request);
}
As you can get values like this below.
{
"OTP":"3245234",
"UserName":"mohit840",
"type":"SuperAdmin"
}
This is a object which i'm sending. Here is the receiving method.
jObject["OTP"].ToString();
public async Task<dynamic> PostAsync([FromBody] Object post)
{
var data = JsonConvert.DeserializeObject<dynamic>(post.ToString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With