首页 > 解决方案 > what is $"type" in a json object and How to deserialize it to my model

问题描述

So i have made an api call and received this object back

[
    {
        "$type": "foo.Api.bar.Entities.foo, foo.Api.bar.Entities",
        "id": "a2",
        "displayName": "A2",
        "statusSeverity": "Good",
        "statusSeverityDescription": "No Exceptional Delays",
        "bounds": "[[-0.0857,51.44091],[0.17118,51.49438]]",
        "envelope": "[[-0.0857,51.44091],[-0.0857,51.49438],[0.17118,51.49438],[0.17118,51.44091],[-0.0857,51.44091]]",
        "url": "/Road/a2"
    }
]

I have no clue what

"$type": "foo.Api.bar.Entities.foo, foo.Api.bar.Entities",

is and i'm not sure how to design my model to take it in?

public class Road
{
    public string Id { get; set; }
    public string DisplayName { get; set; }
    public string StatusSeverity { get; set; }
    public string StatusSeverityDescription { get; set; }
    public string Bounds { get; set; }
    public string Envelope { get; set; }
    public string Url { get; set; }
}

what is $"type" and how should i go about trying to deserialize it into my model, which i will then map into my DTO?

标签: c#jsonserialization

解决方案


The $ sign related to the data source not to JSON itself, to deserialize it use the following :

[JsonProperty(PropertyName = "$type")]
public string PropertyName{ get; set; }

推荐阅读