【问题标题】:How do I return clean JSON from a WCF Service?如何从 WCF 服务返回干净的 JSON?
【发布时间】:2011-01-06 09:33:24
【问题描述】:

我正在尝试从 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? 谢谢!

【问题讨论】:

  • SOAP 应该返回 XML。您可以使用 REST 端点返回 JSON。看看stackoverflow.com/questions/186631/…
  • 顺便说一句,如果其他人遇到这个并想知道为什么“d”属性在那里,它可以修补JSON vulnerability。删除它会让你再次变得脆弱。
  • @Alex - 漏洞取决于重新定义 Array 对象,这在现代浏览器中不再可能。见stackoverflow.com/questions/16289894/…
  • 那很好。 :) 我的答案有一半仍然是正确的——它在那里修补了那个漏洞。

标签: wcf json


【解决方案1】:

将 GetResults 的返回类型更改为 List&lt;Person&gt;
消除用于将 List 序列化为 json 字符串的代码 - WCF 会自动为您执行此操作。

使用您对 Person 类的定义,此代码适用于我:

public List<Person> GetPlayers()
{
    List<Person> players = new List<Person>();
    players.Add(new  Person { FirstName="Peyton", LastName="Manning", Age=35 } );
    players.Add(new  Person { FirstName="Drew", LastName="Brees", Age=31 } );
    players.Add(new  Person { FirstName="Brett", LastName="Favre", Age=58 } );

    return players;
}

结果:

[{"Age":35,"FirstName":"Peyton","LastName":"Manning"},  
 {"Age":31,"FirstName":"Drew","LastName":"Brees"},  
 {"Age":58,"FirstName":"Brett","LastName":"Favre"}]

(全部在一行上)

我也在方法上使用了这个属性:

[WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "players")]

WebInvoke with Method="GET" 与 WebGet 相同,但由于我的一些方法是 POST,因此我使用所有 WebInvoke 以保持一致性。

UriTemplate 设置方法可用的 URL。所以我可以做一个 GET on http://myserver/myvdir/JsonService.svc/players 它只是工作。

还可以查看 IIRF 或其他 URL 重写器以摆脱 URI 中的 .svc。

【讨论】:

  • Cheeso - 我在发布这个问题之前尝试了这种方法。当我使用这种方法时,我收到一条错误消息,提示“使用 'UriTemplate' 的端点不能与 'System.ServiceModel.Description.WebScriptEnablingBehavior' 一起使用。”我究竟做错了什么?谢谢!
  • 在你的 .config 文件中使用 而不是
  • 好的,我将 替换为 并且有效。
  • MGowen - 仅供参考,提出新问题时最好的办法是...打开一个新问题,而不是将问题作为对旧答案的评论发布。
  • Favre 看到你在那里做了什么。
【解决方案2】:

如果您想要漂亮的 json 而不将属性硬编码到您的服务类中,

在你的行为配置中使用&lt;webHttp defaultOutgoingResponseFormat="Json"/&gt;

【讨论】:

    【解决方案3】:

    这是在 web.config 中为您的 web 服务完成的。将 bindingBehavior 设置为 ,您将看到干净的 JSON。额外的“[d]”由您需要覆盖的默认行为设置。

    另请参阅此博文: http://blog.clauskonrad.net/2010/11/how-to-expose-json-endpoint-from-wcf.html

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        当你使用 GET 方法时 合同一定是这样的。

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

        这样我们就有了一个没有引导参数的json

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

        【讨论】:

          【解决方案6】:

          在您的 IServece.cs 添加以下标签:BodyStyle = WebMessageBodyStyle.Bare

           [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "Getperson/{id}")]
          
              List<personClass> Getperson(string id);
          

          【讨论】:

          • 你能解释一下为什么 BodyStyle 会影响结果吗?
          猜你喜欢
          • 2011-10-18
          • 1970-01-01
          • 1970-01-01
          • 2019-05-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-09
          • 1970-01-01
          相关资源
          最近更新 更多