第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從WCF服務(wù)返回干凈的JSON?

如何從WCF服務(wù)返回干凈的JSON?

如何從WCF服務(wù)返回干凈的JSON?我正在嘗試從WCF服務(wù)返回一些JSON。這個服務(wù)只是從我的數(shù)據(jù)庫中返回一些內(nèi)容。我可以得到數(shù)據(jù)。但是,我擔(dān)心JSON的格式。當(dāng)前,返回的JSON格式如下:{"d":"[{\"Age\":35,\"FirstName\":\"Peyton\",\"LastName\":\"Manning\"},{\"Age\":31,\"FirstName\":\"Drew\",\"LastName\":\"Brees\"},{\"Age\":29,\"FirstName\":\"Tony\",\"LastName\":\"Romo\"}]"}實際上,我希望我的JSON格式盡可能清晰。我認(rèn)為(我可能不正確),相同的結(jié)果集合(用干凈的JSON表示)應(yīng)該如下所示:[{"Age":35,"FirstName":"Peyton","LastName":"Manning"},{"Age":31,"FirstName":"Drew","LastName":"Brees"},{"Age":29,"FirstName":"Tony","LastName":"Romo"}]我不知道“d”是從哪里來的。我也不知道為什么要插入轉(zhuǎn)義字符。我的實體看起來如下:[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;     }}負(fù)責(zé)返回內(nèi)容的服務(wù)定義為:[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服務(wù)返回“干凈”JSON?謝謝!
查看完整描述

3 回答

?
開心每一天1111

TA貢獻1836條經(jīng)驗 獲得超13個贊

將GetResults的返回類型更改為List<Person>.
消除用于將列表序列化為json字符串的代碼-WCF自動為您執(zhí)行此操作。

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

結(jié)果:

[{"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")]

方法=“get”的WebInvoke與WebGet相同,但是由于我的一些方法是POST,所以我使用所有WebInvoke來保持一致性。

UriTemplate設(shè)置方法可用的URL。這樣我就能http://myserver/myvdir/JsonService.svc/players它只是起作用了。

還可以查看IIRF或其他URL重寫器,以擺脫URI中的.svc。


查看完整回答
反對 回復(fù) 2019-06-28
?
喵喵時光機

TA貢獻1846條經(jīng)驗 獲得超7個贊

如果您想要在服務(wù)類中沒有硬編碼屬性的良好json,

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


查看完整回答
反對 回復(fù) 2019-06-28
  • 3 回答
  • 0 關(guān)注
  • 1703 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號