How do I convert a complex json object to CLR object with json.net?

折月煮酒 提交于 2019-12-07 15:29:22

Json.Net needs some help because your json object contain propery names, which is not valid in c# like(Agent.Id)

var obj  = JsonConvert.DeserializeObject<MyObj>(json);

How do I convert the json string to object (with inner objects)?

Since your json is flat(not containing sub objects) you have to post process your deserialized object if you want to use it that way/


public class MyObj
{
    public string Id { get; set; }

    [JsonProperty("Parent.Id")]
    public string ParentId { get; set; }

    [JsonProperty("Agent.Id")]
    public string AgentId { get; set; }

    [JsonProperty("Agent.Profile.FullName")]
    public string ProfileFullName { get; set; }

    public string Fee { get; set; }

    public string FeeManagementDate { get; set; }

    [JsonProperty("Contact.Name")]
    public string ContactName { get; set; }

    [JsonProperty("Contact.Telephone")]
    public string ContactTelephone { get; set; }

    [JsonProperty("Contact.Email")]
    public string ContactEmail { get; set; }

    public string AgreementUrl { get; set; }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!