Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic c# ValueKind = Object

As i'm trying to access object values using JsonSerializer.Deserialize using debugger.
enter image description here

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.
enter image description here

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"
}
like image 778
Naman Kumar Avatar asked Jul 27 '20 19:07

Naman Kumar


3 Answers

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.

like image 60
Ankit Mori Avatar answered Nov 19 '22 19:11

Ankit Mori


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();
like image 22
Naman Kumar Avatar answered Nov 19 '22 19:11

Naman Kumar


public async Task<dynamic> PostAsync([FromBody] Object post)
{
     var data = JsonConvert.DeserializeObject<dynamic>(post.ToString());
like image 5
realPro Avatar answered Nov 19 '22 19:11

realPro