1. Create a new WebService.
2.
1 /// <summary>2 ///Summary description of Demotojson3 /// </summary>4[WebService (Namespace ="http://tempuri.org/", description= ("<br><p > XI ' an XX company </p> technical support: Wang Guangxu"))]5[WebServiceBinding (ConformsTo =wsiprofiles.basicprofile1_1)]6[System.ComponentModel.ToolboxItem (false)]7 //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. 8 [System.Web.Script.Services.ScriptService]9 Public classDemoToJson:System.Web.Services.WebServiceTen { One /// <summary> A ///This is a test WebService return JSON format data method - /// </summary> - /// <returns></returns> the[WebMethod (Description ="This is a test WebService return JSON format data method")] -[Scriptmethod (Usehttpget =true, Responseformat =Responseformat.json)] - //[Scriptmethod] - Public voidHelloWorld () + { - Context.Response.Clear (); +Context.Response.ContentType ="Application/json"; AJavaScriptSerializer JSON =NewJavaScriptSerializer (); at varList =NewList<tempjson>() - { - NewTempjson () {id=1, name="Zhang San", age= -, sex="male"}, - NewTempjson () {id=1, name="John Doe", age=1, sex="male"}, - NewTempjson () {id=1, name="Harry", age=2, sex="male"}, - NewTempjson () {id=1, name="Zhao Liu", age= -, sex="male"}, in NewTempjson () {id=1, name="Sun Eight", age= -, sex="male"} - }; to + varJsonlist =New{result ="return Success", Count =list. Count, list}; - the stringResultStr =JSON. Serialize (jsonlist); * Context.Response.Write (RESULTSTR); $ Context.Response.End ();Panax Notoginseng } - } the + [Serializable] A Public classTempjson the { + Public intId {Get;Set; } - Public stringName {Get;Set; } $ Public intAge {Get;Set; } $ Public stringSex {Get;Set; } -}
3. Post-run results:
4. When the current address is refreshed, the error will be as follows:
Workaround:
When the test is not added, the JSON data returned by the Apple development call will be appended with some XML information and parsed normally.
WebService return JSON format data for Apple or Android program calls