I have an application/json
response from an API that has a property, itself, containing an escaped JSON string.
{
"id": 0,
"aggregation_id": "533741f4-49da-4db9-9660-4ca7bafb30e1",
"task_id": "217",
"event_type": "discovery",
"event_name": "device_discovery_complete",
"method": "ssh",
"message_details": "{\"aggregation_id\":\"533741f4-49da-4db9-9660-4ca7bafb30e1\",\"ou_id\":0,\"device_id\":13,\"node_id\":13,\"task_id\":217}",
"time": "2018-01-25T17:59:25"
}
I want to deserialize the object and the inner object to a model type.
public class Response
{
public DateTime time {get; set;}
public string event_name {get; set;}
public string event_type {get; set;}
public string method {get; set;}
public MessageDetails message_details {get; set;}
}
public class MessageDetails
{
public int device_id {get; set;}
}
Using a call like this
JsonConvert.DeserializeObject<Response>("... response string...");
However, Netwonsoft.Json handles the outer properties just fine, but throws an exception on matching message_details
.
Newtonsoft.Json.JsonSerializationException: Error converting value "... response string snipped ..." to type 'RpcApi.Entities.MessageDetails'.
Path '[0].message_details', line 1, position 390.
---> System.ArgumentException: Could not cast or convert from System.String to RpcApi.Entities.MessageDetails.