Parsing a complex JSON result with C#

后端 未结 4 1471
一生所求
一生所求 2021-01-21 08:46

I am trying to parse the following complex JSON result, which is returned from the Zoho Crm API:

{
\"response\":
{
    \"result\":
    {
        \"Contacts\":
           


        
4条回答
  •  孤独总比滥情好
    2021-01-21 09:26

    Use the below classes for de-serializing using JSON.Net

    public class ResponseActual
    {
    
        [JsonProperty("response")]
        public Response2 Response { get; set; }
    }
    
    public class Response2
    {
    
        [JsonProperty("result")]
        public Result Result { get; set; }
    
        [JsonProperty("uri")]
        public string Uri { get; set; }
    }
    
    public class Result
    {
    
        [JsonProperty("Contacts")]
        public Contacts Contacts { get; set; }
    }
    
    public class Contacts
    {
    
        [JsonProperty("row")]
        public IList Row { get; set; }
    }
    
      public class Row
    {
    
        [JsonProperty("no")]
        public string No { get; set; }
    
        [JsonProperty("FL")]
        public IList FL { get; set; }
    }
    
     public class FL
    {
    
        [JsonProperty("content")]
        public string Content { get; set; }
    
        [JsonProperty("val")]
        public string Val { get; set; }
    }
    
    //To De-serialize
    ResponseActual respone = JsonConvert.DeserializeObject(jSON_sTRING)
    
    //Get the contacts list
    List contacts = respone.Response.Result.Contacts.Row[0].FL.ToList();
    
    //Now Get the required value using LINQ
    var value = contacts.Where((s, e) => s.Val =="Email").Select(x=>x.Content).Single();
    

    You may also checkout this - Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

提交回复
热议问题