Parsing a complex JSON result with C#

后端 未结 4 1442
一生所求
一生所求 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:25

    Can I ask you something? Are you the one exporting the JSON? I ask this because the format is quite weird and it does get in the way of your code.

    A simpler format would allow you to serialize the string pretty much in a direct way.

    0 讨论(0)
  • 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> Row { get; set; }
    }
    
      public class Row
    {
    
        [JsonProperty("no")]
        public string No { get; set; }
    
        [JsonProperty("FL")]
        public IList<FL> 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<ResponseActual>(jSON_sTRING)
    
    //Get the contacts list
    List<FL> contacts = respone.Response.Result.Contacts.Row[0].FL.ToList();
    
    //Now Get the required value using LINQ
    var value = contacts.Where<FL>((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?)

    0 讨论(0)
  • 2021-01-21 09:31

    You need to include the following portion of JSON in your deserialization object Type :

    {
    "response":
    {
        "result":
        {
            "Contacts":
            {
                "row":
                [
                    {
                        "no":"1",
                        "FL":
    

    Class type : 'Contact' is inadequate.

    0 讨论(0)
  • 2021-01-21 09:38
    you can use this code:
    
        dynamic dictionary = (JsonConvert.DeserializeObject>(jsonstring))["response"];
    
    
                var result = dictionary.result;
                var contact= result.Contacts;
                var row= contact.row;
    
                foreach (var item in row)
                {
    
                    var no= item.no;
    
                }
    
    0 讨论(0)
提交回复
热议问题