-1

code

public string getallcustomer()
        {
            DataTable dt = new DataTable();
            CustomerService _customerService = new CustomerService();

            dt = _customerService.Getallcustomer();
            List<Dictionary<string, object>> lstPersons = _customerService.GetTableRows(dt);
           // var json = JsonSerializer.Serialize(lstPersons);
            //return Json(lstPersons, JsonRequestBehavior.AllowGet);
            string json = JsonConvert.SerializeObject(lstPersons);

            return json;
        }

output:

"[{\"FirstName\":\"gateway\",\"PhoneNumber\":\"\",\"Balance\":-10.0000,\"CompanyName\":\"gateway\",\"Email\":\"1gateway@Sipkernel.com\",\"CustomerID\":1},{\"FirstName\":\"a-Office\",\"PhoneNumber\":null,\"Balance\":20.0000,\"CompanyName\":\"a-Office\",\"Email\":\"office@a.com\",\"CustomerID\":2}]"
dbc
  • 104,963
  • 20
  • 228
  • 340
Prince
  • 277
  • 2
  • 16
  • https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_JsonConvert_SerializeObject.htm this how it looks string representation of json – teju c Nov 30 '17 at 04:26
  • 1
    Looks to be a duplicate of [JSON.NET Parser *seems* to be double serializing my objects](https://stackoverflow.com/q/25559179/3744182). Rather than serializing manually and returning the JSON string, return `lstPersons` itself (or some appropriate [DTO](https://en.wikipedia.org/wiki/Data_transfer_object)) and let the framework do the serialization. – dbc Nov 30 '17 at 04:31

2 Answers2

0
i got the solution.successfully done.guys no need to serialize the list objects again.I can post the codes here.No need to return as string.
//controller





  public List<getallaccountDto> getallcustomer()
            {
                DataTable dt = new DataTable();
                CustomerService _customerService = new CustomerService();
                dt = _customerService.Getallcustomer();
                return _customerService.GetTableRows(dt);
            }

    //model dto

 public class GetAllCustomersDto
    {
        public List<getallaccountDto> accounts { get; set; }
    }
    //business layer

    //modified prince


   public List<getallaccountDto> GetTableRows(DataTable dtData)
        {
            List<getallaccountDto> customerList = new List<getallaccountDto>();
            getallaccountDto customer = null;        
                foreach (DataRow dr in dtData.Rows)
                {
                customer = new getallaccountDto();
                customer.Name = dr["FirstName"].ToString();
                customer.Phone = dr["PhoneNumber"].ToString();
                customer.Balance = dr["Balance"].ToString();
                customer.company = dr["CompanyName"].ToString();
                customer.Email = dr["Email"].ToString();
                customer.CustomerID = dr["CustomerID"].ToString();
                customerList.Add(customer);
            }                

            return customerList;
        }

   // this will return correct json responses.i have tested it with postman and swagger...successfully done;






 [
      {
        "CustomerID": "1",
        "Name": "gateway",
        "Phone": "",
        "Balance": "-10.0000",
        "company": "gateway",
        "Email": "1gateway@Sipkernel.com"
      },
      {
        "CustomerID": "2",
        "Name": "IPsmarx-Office",
        "Phone": "",
        "Balance": "20.0000",
        "company": "IPsmarx-Office",
        "Email": "office@ipsmarx.com"
      },
      {
        "CustomerID": "3",
        "Name": "khan",
        "Phone": "",
        "Balance": "23.0000",
        "company": "Test",
        "Email": "srk@king.com"
      }]
Prince
  • 277
  • 2
  • 16
-1

That is because you are returning a string and the web api framework is serializing the string into JSON string literal so the entire thing is wrapped in double quotes (and quotes within are escaped using a backslash).

Fix

Change your action so it is returning IHttpActionResult: This is better anyways in case you wanted to return an error or something else.

public IHttpActionResult GetAllCustomers()
{
    // code....
    return Json(lstPersons);
}
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64