Return JSON format
//using System.Web.Script.Services;[WebMethod] [Scriptmethod (Usehttpget=true, Responseformat =Responseformat.json)] Public voidHelloWorld () {Context.Response.Clear (); Context.Response.ContentType="Application/json"; Model.User.User_User User=NewModel.User.User_User (); User. UserName="we are"; User. UID=1; User. UserPassword="123456"; Jayrock.Json.JsonTextWriter writer=NewJayrock.Json.JsonTextWriter (); Jayrock.Json.Conversion.JsonConvert.Export (user, writer); Context.Response.Write (writer. ToString ()); }
Effect:
return XML format
[WebMethod] PublicXmlDocument HelloWorld () {downlist doo=Newdownlist (); List<file> flist =NewList<file>(); File F=Newfile (); F.name="Test"; F.value="T"; Flist. ADD (f); List<sql> slist =NewList<sql>(); List<desc> dlist =NewList<desc>(); Version ver=Newversion (); Doo.version=ver; Doo.sqllist=slist; Doo.filelist=flist; XmlDocument xmldoc=NewXmlDocument (); stringXMLString =Utility.Tool.Serialize (doo); Xmldoc.loadxml (xmlstring); returnxmldoc; }
Public classdownlist { PublicList<file>fileList; PublicList<sql>sqllist; Publicversion version; PublicList<desc>desclist; Public intresult; } Public classSQL {[XmlText] Public stringvalue; } [Serializable] Public classfile {[XmlAttribute] Public stringname; [XmlText] Public stringvalue; } Public classdesc {[XmlText] Public stringvalue; } [Serializable] Public classversion {[XmlAttribute] Public stringname; }
/// <summary> ///serializes the specified object into an XML-formatted string and returns. /// </summary> /// <param name= "O" >objects to serialize</param> /// <returns>returns the serialized string</returns> Public Static stringSerialize (Object o) {stringXML =""; Try{XmlSerializer Serializer=NewXmlSerializer (O.gettype ()); using(MemoryStream mem =NewMemoryStream ()) { using(XmlTextWriter writer =NewXmlTextWriter (Mem, Encoding.UTF8)) {writer. Formatting=formatting.indented; XmlSerializerNamespaces N=Newxmlserializernamespaces (); N.add ("",""); Serializer. Serialize (writer, o, N); Mem. Seek (0, Seekorigin.begin); using(StreamReader reader =NewStreamReader (MEM)) {XML=Reader. ReadToEnd (); } } } } Catch{XML =""; } returnXML; }
Effect: