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

时间:2022-10-24 12:50:48

I am trying to return some JSON from a WCF service. This service simply returns some content from my database. I can get the data. However, I am concerned about the format of my JSON. Currently, the JSON that gets returned is formatted like this:

我正在尝试从WCF服务返回一些JSON。该服务仅从我的数据库返回一些内容。我可以得到数据。但是,我关心的是JSON格式。目前,返回的JSON格式如下:

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

In reality, I would like my JSON to be formatted as cleanly as possible. I believe (I may be incorrect), that the same collection of results, represented in clean JSON, should look like so:

实际上,我希望我的JSON格式尽可能的简洁。我相信(我可能是错的),同样的结果集合,用干净的JSON表示,应该是这样的:

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

I have no idea where the “d” is coming from. I also have no clue why the escape characters are being inserted. My entity looks like the following:

我不知道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;
    }
}

The service that is responsible for returning the content is defined as:

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

[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;
    }
}

How do I return “clean” JSON from a WCF service? Thank you!

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

6 个解决方案

#1


201  

Change the return type of your GetResults to be List<Person>.
Eliminate the code that you use to serialize the List to a json string - WCF does this for you automatically.

将GetResults的返回类型更改为List 。删除用于将列表序列化为json字符串的代码——WCF自动执行此操作。

Using your definition for the Person class, this code works for me:

使用你对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;
}

results:

结果:

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

(All on one line)

(都在一行上)

I also used this attribute on the method:

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

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

WebInvoke with Method= "GET" is the same as WebGet, but since some of my methods are POST, I use all WebInvoke for consistency.

用Method= "GET"的WebInvoke和WebGet是一样的,但是由于我的一些方法是POST,所以我使用所有WebInvoke来保持一致性。

The UriTemplate sets the URL at which the method is available. So I can do a GET on http://myserver/myvdir/JsonService.svc/players and it just works.

UriTemplate设置方法可用的URL。所以我可以在myserver/myvdir/jsonservice上做一个GET。svc/播放器就可以了。

Also check out IIRF or another URL rewriter to get rid of the .svc in the URI.

还可以检查IIRF或其他URL重写器,以删除URI中的.svc。

#2


88  

If you want nice json without hardcoding attributes into your service classes,

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

use <webHttp defaultOutgoingResponseFormat="Json"/> in your behavior config

在你的行为配置中使用

#3


27  

This is accomplished in web.config for your webservice. Set the bindingBehavior to <webHttp> and you will see the clean JSON. The extra "[d]" is set by the default behavior which you need to overwrite.

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

See in addition this blogpost: http://blog.clauskonrad.net/2010/11/how-to-expose-json-endpoint-from-wcf.html

另外,请参阅这篇博文:http://blog.clauskonrad.net/2010/11/how- expose-json-endpoint-from-wcf.html

#4


5  

I faced the same problem, and resolved it by changing the BodyStyle attribut value to "WebMessageBodyStyle.Bare" :

我也遇到了同样的问题,通过改变BodyStyle的方式来解决它,但它的价值是“WebMessageBodyStyle”。裸”:

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

The returned object will no longer be wrapped.

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

#5


1  

When you are using GET Method the contract must be this.

当你使用GET方法时,合同必须是这个。

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

with this we have a json without the boot parameter

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

Aldo Flores @alduar http://alduar.blogspot.com

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

#6


0  

In your IServece.cs add the following tag : BodyStyle = WebMessageBodyStyle.Bare

在你IServece。c添加以下标签:BodyStyle = WebMessageBodyStyle.Bare。

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

    List<personClass> Getperson(string id);

#1


201  

Change the return type of your GetResults to be List<Person>.
Eliminate the code that you use to serialize the List to a json string - WCF does this for you automatically.

将GetResults的返回类型更改为List 。删除用于将列表序列化为json字符串的代码——WCF自动执行此操作。

Using your definition for the Person class, this code works for me:

使用你对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;
}

results:

结果:

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

(All on one line)

(都在一行上)

I also used this attribute on the method:

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

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

WebInvoke with Method= "GET" is the same as WebGet, but since some of my methods are POST, I use all WebInvoke for consistency.

用Method= "GET"的WebInvoke和WebGet是一样的,但是由于我的一些方法是POST,所以我使用所有WebInvoke来保持一致性。

The UriTemplate sets the URL at which the method is available. So I can do a GET on http://myserver/myvdir/JsonService.svc/players and it just works.

UriTemplate设置方法可用的URL。所以我可以在myserver/myvdir/jsonservice上做一个GET。svc/播放器就可以了。

Also check out IIRF or another URL rewriter to get rid of the .svc in the URI.

还可以检查IIRF或其他URL重写器,以删除URI中的.svc。

#2


88  

If you want nice json without hardcoding attributes into your service classes,

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

use <webHttp defaultOutgoingResponseFormat="Json"/> in your behavior config

在你的行为配置中使用

#3


27  

This is accomplished in web.config for your webservice. Set the bindingBehavior to <webHttp> and you will see the clean JSON. The extra "[d]" is set by the default behavior which you need to overwrite.

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

See in addition this blogpost: http://blog.clauskonrad.net/2010/11/how-to-expose-json-endpoint-from-wcf.html

另外,请参阅这篇博文:http://blog.clauskonrad.net/2010/11/how- expose-json-endpoint-from-wcf.html

#4


5  

I faced the same problem, and resolved it by changing the BodyStyle attribut value to "WebMessageBodyStyle.Bare" :

我也遇到了同样的问题,通过改变BodyStyle的方式来解决它,但它的价值是“WebMessageBodyStyle”。裸”:

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

The returned object will no longer be wrapped.

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

#5


1  

When you are using GET Method the contract must be this.

当你使用GET方法时,合同必须是这个。

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

with this we have a json without the boot parameter

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

Aldo Flores @alduar http://alduar.blogspot.com

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

#6


0  

In your IServece.cs add the following tag : BodyStyle = WebMessageBodyStyle.Bare

在你IServece。c添加以下标签:BodyStyle = WebMessageBodyStyle.Bare。

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

    List<personClass> Getperson(string id);