Search code examples
c#jsonodata

How do I Deserialize OData JSON Error Message?


I'm trying to deserialize this odata error message:

enter image description here

The object members are always null for this code:

    public static async Task<ExceptionResponse> ExceptionResponse(this HttpResponseMessage httpResponseMessage)
    {
        string responseContent = await httpResponseMessage.Content.ReadAsStringAsync();
        ExceptionResponse exceptionResponse = JsonConvert.DeserializeObject<ExceptionResponse>(responseContent);
        return exceptionResponse;
    }
}

public class ExceptionResponse
{
    public string Message { get; set; }
    public string ExceptionMessage { get; set; }
    public string ExceptionType { get; set; }
    public string StackTrace { get; set; }
    public ExceptionResponse InnerException { get; set; }
}

How can I get the error message and innererror message?


Solution

  • Try this

    public class InnerError {
        public string Message { get; set; }
        public string Type { get; set; }
        public string StackTrace { get; set; }
    }
    
    public class Error {
        public string Code { get; set; }
        public string Message { get; set; }
        public InnerError InnerError { get; set; }
    }
    
    public class ExceptionResponse {
        public Error Error { get; set; }
    }