{
"correlationID": "00000000-0000-0000-0000-000000000000",
"scenarioID": 2,
"scenarioIsAcceptedInPrinciple": false,
"valid": false,
"errors": [
{
"errorID": 90,
"errorText": "For the 1st Employment of the 1st Applicant Please provide a valid Landline"
},
{
"errorID": 22,
"errorText": "The provided value 'string' is not a valid value for the property 'applicants[0][email]'"
}
]
}
I have this JSON response that is produced from a POST request that I have sent.
I would like to deseralise it so I can call the errorID and errorText and do testing assertions to see if the correct error ID and text came back.
I have got it to work with;
public partial class postRequest
{
public bool Valid { get; set; }
public object Errors { get; set; }
}
and it returns all of the error, like this;
Standard Output:
[
{
"errorID": 90,
"errorText": "For the 1st Employment of the 1st Applicant Please provide a valid Landline"
},
{
"errorID": 22,
"errorText": "The provided value 'string' is not a valid value for the property 'applicants[0][email]'"
}
]
How would I return just the error ID and error text.,
Thanks
CodePudding user response:
You should create another class for the error.
public class Error
{
public string ErrorID { get; set; }
public string ErrorText { get; set; }
}
And then use that in the other class:
public Error[] Errors { get; set; }
Finally you access the errors like:
response.Errors[0].ErrorID
CodePudding user response:
You have to deserialize it like this object :
public class JsonResponse
{
public bool valid { get; set; }
public List<Error> errors { get; set; }
}
public class Error
{
public int errorID { get; set; }
public string errorText { get; set; }
}
For deserialization:
var data =System.Text.Json.JsonSerializer.Deserialize<JsonResponse>("your json source");
CodePudding user response:
you have a list of errors, not just a one
List<Error> errors = JObject.Parse(json)["errors"].Select(x => x.ToObject<Error>()).ToList();
public class Error
{
public int ErrorID { get; set; }
public string ErrorText { get; set; }
}
you can see them
foreach (var error in errors)
{
Console.WriteLine($"Error Id : {error.ErrorID}, Error Text {error.ErrorText}");
}
// or
var errId=errors[0].ErrorID;