I convert json to object and get this error
Newtonsoft.Json.JsonSerializationException: 'Error converting value "{"mid":"2","phone":"0965403801","requestId":"1585970510.732827","bankHubToken":"eyJ0aW1lc3RhbXAiOiIxNjMzNDE4NzgyNzQ0IiwidG9rZW4iOiI5NWJmYWM3OC00YmY3LTQyM2MtYjAxNS00NTNjMjkzYzk1ZWEiLCJzaWduIjoiLzVrcTg2dWdReFUvQ1daWCt2TERSaGhvaXN1VC9hS1JpejEwUGtTU0dxUVx1MDAzZCJ9","userName":"1392H46"}" to type 'OBBE.DGBIZ.API.Model.DataFieldUploadFile'. Path '', line 1, position 305.'
my code
var data = context.ActionArguments["data"];
if (data == null)
{
context.Result = new OkObjectResult(
new ErrorResponseMessage() { context = new ContextResponse(false, new List<string>() { APPID, HANDER_ERRORCODE.ERR_REQUEST_CONTENT.ToString(), "Request null" }, new ContextRequest("", ""), ResultConstants.ERROR_CODE_FAIL, "Request null") });
return;
}
try
{
String x = JsonConvert.SerializeObject(data);
data = JsonConvert.DeserializeObject<DataFieldUploadFile>(JsonConvert.SerializeObject(data));
}
and my class
public class DataFieldUploadFile
{
public String? mid { get; set; }
public String? requestId { get; set; }
public String? phone { get; set; }
public String? bankHubToken { get; set; }
public String? userName { get; set; }
}
I try but not OK.
CodePudding user response:
try this
DataFieldUploadFile data = context.ActionArguments["data"] as DataFieldUploadFile;
if data is null it means that data is a probably a json string already, and try to double serialize it.
So you really need this code
string json = context.ActionArguments["data"]?.ToString();
if (!string.IsNullOrEmpty(json))
{
var data = JsonConvert.DeserializeObject<DataFieldUploadFile>(json);
}
else ... you another code