Strange response from WCF service, how to return json easily

Posted by Exitos on Stack Overflow See other posts from Stack Overflow or by Exitos
Published on 2011-11-12T16:48:27Z Indexed on 2011/11/12 17:51 UTC
Read the original article Hit count: 150

Filed under:
|

I want to get a service to respond with just JSON. I have written the following code:

namespace BM.Security
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AssocFileService
    {


        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        public List<Person> GetPeople(int message)
        {

            List<Person> myList = new List<Person>();

            Person p = new Person() 
            { 
                Age = 28,
                Name="Name1"
            };
            Person p2 = new Person()
            {
                Age = 26,
                Name = "Name2"
            };

            myList.Add(p);
            myList.Add(p2);



            return myList;
        }

    }

    [DataContract]
    public class Person
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public int Age { get; set; }
    }
}

But im getting the following JSON back which is really wierd...

{ "d" : [ { "Age" : 28,
        "Name" : "Name1",
        "__type" : "Person:#Bm.Security"
      },
      { "Age" : 26,
        "Name" : "Name2",
        "__type" : "Person:#BM.Security"
      }
    ] }

I'm totally stumped by the "d" no idea where that has come from. And also by the __type variable, no thanks don't really want that in my Json :-( How do I set the root node in my data to replace that d? Where did the d come from? So many questions...

Hope someone can help....

© Stack Overflow or respective owner

Related posts about wcf

Related posts about JSON