Return JSON Array from API in ASP.Net

lakshmi.teli95
 
on Nov 18, 2022 12:21 AM
429 Views

we are creating getdetails by id api

public async Task<EntityResponseModel> GetContactByIdAsync(int Id)
{
    var contactResponse = new List<ContactResponse>();
    Contact contact = null;
    this.pageNo = pageNo;
    this.pageSize = pageSize;
    var statusCode = 200;
    var contacts = (await repository.FindAsync(GetSpecification())).Where(s => s.Id == Id)
        .Select(s => new Contact
         {
             Id = s.Id,
             FirstName = s.FirstName,
             Lastname = s.Lastname,
             Email = s.Email,
             Phone = s.Phone,
             Mobile = s.Mobile,
             Gender = s.Gender,
             Country = s.Country,
             State = s.State,
             City = s.City,
             Street = s.Street,
             Zip = s.Zip,
             DOB = s.DOB
         }).ToList();
    int count = await repository.CountAsync(v => !v.IsDeleted);
    var _pages = Math.Round(count / (double)pageSize, MidpointRounding.ToPositiveInfinity);
    contactResponse = _mapper.Map<List<Contact>, List<ContactResponse>>(contacts);
 
    return new EntityResponseModel
    {
        APIResponse = APIResponseHelper.Getinstance().ConvertToAPIResponse(contactResponse, string.Empty, statusCode),
        Entity = contacts,
        EntityResponse = contactResponse
    };
}

its output is coming in this format

{
  "statusCode": 200,
  "data": [
    {
      "FirstName": "Manju",
      "Lastname": "Teli",
      "Email": "Manju@gmail.com",
      "Phone": "7867676556",
      "Mobile": "6675655612",
      "Gender": 0,
      "Country": "usa",
      "State": "newyork",
      "City": "xyz",
      "Street": "tfgsas",
      "Zip": "6578",
      "DOB": "1997-05-03T04:37:47.935",
      "id": 1,
      "isActive": false
    }
  ],
  "message": "",
  "TotalPages": 0,
  "TotalCount": 0
}

I don't want data in curly braces, how to change this

Please any one suggest me how to get the answer in [] and not in {}

I want like this, but in above code what i have to change

{
  "statusCode": 200,
  "data": [
      "Manju",
      "Teli",
      "Manju@gmail.com",
      "7867676556",
      "6675655612",
      0,
      "usa",
      "newyork",
      "xyz",
      "tfgsas",
      "6578",
      "1997-05-03T04:37:47.935",
      1,
      false
  ],
  "message": "",
  "TotalPages": 0,
  "TotalCount": 0
}
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Nov 18, 2022 06:40 AM
lakshmi.teli95 says:
    var contacts = (await repository.FindAsync(GetSpecification())).Where(s => s.Id == Id)
        .Select(s => new Contact
         {
             Id = s.Id,
             FirstName = s.FirstName,
             Lastname = s.Lastname,
             Email = s.Email,
             Phone = s.Phone,
             Mobile = s.Mobile,
             Gender = s.Gender,
             Country = s.Country,
             State = s.State,
             City = s.City,
             Street = s.Street,
             Zip = s.Zip,
             DOB = s.DOB
         }).ToList();

Replace above with below code.

var contacts = (await repository.FindAsync(GetSpecification())).Where(s => s.Id == Id)
.Select(s => new List<string>
{
    s.Id,
    s.FirstName,
    s.Lastname,
    s.Email,
    s.Phone,
    s.Mobile,
    s.Gender,
    s.Country,
    s.State,
    s.City,
    s.Street,
    s.Zip,
    s.DOB
}).FirstOrDefault().ToArray();