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

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Akshay Deodhar. Thank You. This solution works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.