mohan yadav

mohan yadav

  • NA
  • 198
  • 719

Why JSON convert into javascript object.

Apr 14 2016 3:06 AM
I have created web api which return JSON like below web api methods return list of book class
public HttpResponseMessage GetAllBooks()
{
list.Add(new Book() { Id = 1, Name = "Book1", Price = 0 });
list.Add(new Book() { Id = 2, Name = "Book2", Price = 0 });
HttpResponseMessage ob = new HttpResponseMessage();
return Request.CreateResponse(HttpStatusCode.Created, list);
//return list;
}
 
 When i call it from ajax call it returns in javascript object like 
resultArray[2]
  result[0].ID, result[0].Name, result[0].Price and so on.
 
I am expectiing JSON like 
 
[
{
"Id": 1,
"Name": "Book1",
"Price": 0,
},
{
"Id": 2,
"Name": "Book1",
"Price": 0,
},]
 
 
for the same i have to use JSON.Stringfy in ajax call.
 
Is there a way  to get above JSON directly from web api or WCF rest.
 
 
 
 

Answers (4)