Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need a JSON response like this:

{
        "statusCode": 200,
        "errorMessage": "Success",
        "Employees": [
                {
                "empid": "7228",
                "name": "Tim",
                "address": "10815 BALTIMORE",
                
                },
               {
                "empid": "7240",
                "name": "Joe",
                "address": "10819 Manasas",
                }
            ]
}


Model Class:

C#
public class EmployeeList
{
    public string empid { get; set; }
    public string name { get; set; }
    public string address { get; set; }
}


public class Employee
{
     public int statusCode { get; set; }
     public string errorMessage{ get; set; }
     public List<EmployeeList> Employees { get; set; }
}


What I have tried:

Controller is like this:

C#
List<Models.Employee> emp = new List<Models.Employee>();
//call to SP
if (rdr.HasRows)
    {
           
                var okresp = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    ReasonPhrase = "Success"
                };

                Models.Employee item = new Models.Employee();
                {
                    item.statusCode = Convert.ToInt32(okresp.StatusCode);
                    item.errorMessage = okresp.ReasonPhrase;
               
                    
                    while (rdr.Read())
                    {
                        item.Employees = new List<EmployeeList>{
                        new EmployeeList
                        { 
                            empid = Convert.ToString(rdr["empid"]),
                            name = Convert.ToString(rdr["name"]),
                            address = Convert.ToString(rdr["address"]) 
                        }};
                     }
                    
                };

    emp.add(item);

// return response


I am unable to get the loop part of creating JSON array in response file. I'm getting only the last value of Employees list from reader in my JSON response. Am I assigning the values in While loop incorrectly? Is it the correct way to return List as JSON array?
Posted
Updated 15-Sep-20 5:24am

You are creating a new List<EmployeeList> for each record that you read. You should create the List first. Then for each record that is read, create a new Employee item(see below) and add it to the list.

Also, your EmployeeList class is not a List but your Employee class contains a List, which is quite confusing. You should rename both classes to reflect exactly what they are supposed to represent.
 
Share this answer
 
v2
Comments
Member 14938958 15-Sep-20 11:19am    
Thanks Richard, true, the naming is incorrect, its for illustration purpose. Also, you mentioned to "create a new Employee item(see below)" I suppose u missed to add some code here? correct me if I am wrong.
Richard MacCutchan 15-Sep-20 11:25am    
"the naming is incorrect, its for illustration purpose." Well that makes no sense at all, it just makes it more difficult for us to help you.

"Also, you mentioned to "create a new Employee item(see below)" I suppose u missed to add some code here?" Sorry, but I do not understand what you are asking.
You need something like;

Models.Employee item = new Models.Employee();
                {
                    item.statusCode = Convert.ToInt32(okresp.StatusCode);
                    item.errorMessage = okresp.ReasonPhrase;
                    item.Employees = new List<EmployeeList>();
                    
                    while (rdr.Read())
                    {
                        item.Employees.Add (new EmployeeList
                        { 
                            empid = Convert.ToString(rdr["empid"]),
                            name = Convert.ToString(rdr["name"]),
                            address = Convert.ToString(rdr["address"]) 
                        });
                     }
                    
                };


that way the list is created once and then added to with each Read. Your original code simply overrode the previous list with a new one on each Read, leaving on the final item.
 
Share this answer
 
Comments
Member 14938958 15-Sep-20 11:36am    
You are correct! Thanks a lot.
I did try to add each item like "item.Employees.Add (new EmployeeList{});" previously but was getting an internal server error, then i changed the code.. didn't realize I had to add a list "item.Employees = new List<employeelist>();". Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900