如何从WCF服务返回干净的JSON?

如何从WCF服务返回干净的JSON?

我正在尝试从WCF服务返回一些JSON。这个服务只是从我的数据库中返回一些内容。我可以得到数据。但是,我担心JSON的格式。当前,返回的JSON格式如下:

{"d":"[{\"Age\":35,\"FirstName\":\"Peyton\",\"LastName\":\"Manning\"},{\"Age\":31,\"FirstName\":\"Drew\",\"LastName\":\"Brees\"},{\"Age\":29,\"FirstName\":\"Tony\",\"LastName\":\"Romo\"}]"}

实际上,我希望我的JSON格式尽可能清晰。我认为(我可能不正确),相同的结果集合(用干净的JSON表示)应该如下所示:

[{"Age":35,"FirstName":"Peyton","LastName":"Manning"},{"Age":31,"FirstName":"Drew","LastName":"Brees"},{"Age":29,"FirstName":"Tony","LastName":"Romo"}]

我不知道“d”是从哪里来的。我也不知道为什么要插入转义字符。我的实体看起来如下:

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

    [DataMember]
    public string LastName { get; set; }

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

    public Person(string firstName, string lastName, int age)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Age = age;
    }}

负责返回内容的服务定义为:

[ServiceContract(Namespace = "")][AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]public class TestService{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public string GetResults()
    {
        List<Person> results = new List<Person>();
        results.Add(new Person("Peyton", "Manning", 35));
        results.Add(new Person("Drew", "Brees", 31));
        results.Add(new Person("Tony", "Romo", 29));

        // Serialize the results as JSON
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(results.GetType());
        MemoryStream memoryStream = new MemoryStream();
        serializer.WriteObject(memoryStream, results);

        // Return the results serialized as JSON
        string json = Encoding.Default.GetString(memoryStream.ToArray());
        return json;
    }}

如何从WCF服务返回“干净”JSON?谢谢!


慕哥6287543
浏览 1612回答 3
3回答

开心每一天1111

将GetResults的返回类型更改为List<Person>.消除用于将列表序列化为json字符串的代码-WCF自动为您执行此操作。使用Person类的定义,此代码适用于我:public&nbsp;List<Person>&nbsp;GetPlayers(){ &nbsp;&nbsp;&nbsp;&nbsp;List<Person>&nbsp;players&nbsp;=&nbsp;new&nbsp;List<Person>(); &nbsp;&nbsp;&nbsp;&nbsp;players.Add(new&nbsp;&nbsp;Person&nbsp;{&nbsp;FirstName="Peyton",&nbsp;LastName="Manning",&nbsp;Age=35&nbsp;}&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;players.Add(new&nbsp;&nbsp;Person&nbsp;{&nbsp;FirstName="Drew",&nbsp;LastName="Brees",&nbsp;Age=31&nbsp;}&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;players.Add(new&nbsp;&nbsp;Person&nbsp;{&nbsp;FirstName="Brett",&nbsp;LastName="Favre",&nbsp;Age=58&nbsp;}&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;players;}结果:[{"Age":35,"FirstName":"Peyton","LastName":"Manning"},&nbsp;&nbsp; &nbsp;{"Age":31,"FirstName":"Drew","LastName":"Brees"},&nbsp;&nbsp; &nbsp;{"Age":58,"FirstName":"Brett","LastName":"Favre"}](全部在一条线上)我还在方法上使用了这个属性:[WebInvoke(Method&nbsp;=&nbsp;"GET", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RequestFormat&nbsp;=&nbsp;WebMessageFormat.Json, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ResponseFormat&nbsp;=&nbsp;WebMessageFormat.Json, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UriTemplate&nbsp;=&nbsp;"players")]方法=“get”的WebInvoke与WebGet相同,但是由于我的一些方法是POST,所以我使用所有WebInvoke来保持一致性。UriTemplate设置方法可用的URL。这样我就能http://myserver/myvdir/JsonService.svc/players它只是起作用了。还可以查看IIRF或其他URL重写器,以摆脱URI中的.svc。

喵喵时光机

如果您想要在服务类中没有硬编码属性的良好json,使用<webHttp defaultOutgoingResponseFormat="Json"/>在行为配置中
打开App,查看更多内容
随时随地看视频慕课网APP