0

I am using Newtonsoft package in C#. I am trying to display all the items listed in a nested JSON array. I am having difficulty displaying the Name Jennifer Jones This is what the JSON String Looks Like

"responseDetails": {
        "pageoffset": 0,
        "size": 950,
        
    },
    "data": [
        {
            "id": 473145,
            "name": "Class of 2000",
            "doc_prog": 
               {
                "responseDetails": 
                 {
                    "pageoffset": 0,
                    "size": 1,
                    
                 },
                "data": [
                    {
                        "name": "Jennifer Jones"
                    }
                ]
            },

This is what my class looks like

public respDetails responseDetails { get; set; }

    public class respDetails
    {
        public int pageoffset { get; set; }
        public string size { get; set; }
    }

    public List<datas> data { get; set; } // Top level class attribute

    public class datas
    {
        public int id { get; set; }
        public string name__v { get; set; }            
        public Programs doc_prog { get; set; }

        public class Programs
        {
            public respDetails responseDetails { get; set; }

            public class respDetails
            {
                public int pageoffset { get; set; }
                public int size { get; set; }
                
            }

            public List<datasprogram> data { get; set; } // Top level class attribute

            public class datasprogram
            {
                public string name { get; set; }
            }


        }     

     }   

This is how I set up the for loop to list all the items in the array

var jRelated = JsonConvert.DeserializeObject<JDocsClass>(strRelated);

            

         
foreach (var num in jRelated.data)
            {
                Console.WriteLine(" Page Offset " + num.doc_prog.responseDetails.pageoffset.ToString() + " " + num.doc_prog.data.ToString() );

                
            }

This is the Program Output

Page Offset 0 System.Collections.Generic.List`1[storeAPI.JCorrespondenceDocsClass+datas+Programs+datasprogram]

So Instead of displaying "Jennifer Jones", I am displaying "System.Collections.Generic.List...."

I appreciate any help pointing me in the right direction

  • 1
    Does this answer your question? [Why am I getting System.Collections.Generic.List\`1\[System.String\] instead of the list's contents?](https://stackoverflow.com/questions/16106181/why-am-i-getting-system-collections-generic-list1system-string-instead-of-the) – Charlieface May 19 '21 at 04:31

1 Answers1

0
num.doc_prog.data.ToString()

Here what you are trying to print is list!

num.doc_prog.data[0].name

This will give you the desired result if there is atleast one element in the list (which is there in your example json), if there are multiple names then to display them you need to loop thru num.doc_prog.data

foreach(datasprogram data in num.doc_prog.data)
{
  string name = data.name;
}