我试图从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 ? 谢谢你!


当前回答

当您使用GET方法时 合同必须是这样的。

[WebGet(UriTemplate = "/", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<User> Get();

这样我们就得到了一个不带boot参数的json

奥尔多·弗洛雷斯 @alduar http://alduar.blogspot.com

其他回答

这是在web中完成的。配置你的webservice。将bindingBehavior设置为<webHttp>,您将看到干净的JSON。额外的“[d]”由您需要覆盖的默认行为设置。

另见这篇博文: http://blog.clauskonrad.net/2010/11/how-to-expose-json-endpoint-from-wcf.html

如果你想要好的json,而不是硬编码属性到你的服务类中,

在行为配置中使用<webHttp defaultOutgoingResponseFormat="Json"/>

在你的iservce .cs中添加以下标签:BodyStyle = WebMessageBodyStyle。裸露的

 [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "Getperson/{id}")]

    List<personClass> Getperson(string id);

当您使用GET方法时 合同必须是这样的。

[WebGet(UriTemplate = "/", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<User> Get();

这样我们就得到了一个不带boot参数的json

奥尔多·弗洛雷斯 @alduar http://alduar.blogspot.com

我遇到了同样的问题,并通过将BodyStyle属性值更改为“WebMessageBodyStyle”来解决它。裸”:

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetProjectWithGeocodings/{projectId}")]
GeoCod_Project GetProjectWithGeocodings(string projectId);

返回的对象将不再被包装。