Tag: javascriptserializer

使用JavaScriptSerializer序列化词典

显然, IDictionary被序列化为KeyValuePair对象的数组(例如, [{Key:”foo”, Value:”bar”}, …] )。 是否可以将其序列化为对象(例如, {foo:”bar”} )?

ASP.NET WebService使用XML标记包装我的JSON响应

我不确定我错过了什么,我错过了什么。 我正在构建一个ASP.NET 2.0(在.Net 3.5框架上)Web应用程序,我正在包含一个Web服务。 请注意,这不是 MVC项目。 我希望公开一个返回JSON字符串的方法; 格式化以提供jqGrid jQuery插件。 这是我在我的服务中实现的初步测试方法:感谢( Phil Haack的MVC指南 ) [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string getData() { JavaScriptSerializer ser = new JavaScriptSerializer(); var jsonData = new { total = 1, // we’ll implement later page = 1, records = 3, // implement later rows = new[]{ new {id = 1, cell […]

Asmx web服务如何返回JSON而不是XML?

我的服务方式: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string getDataFromTrainingMaster() { List results = new DAL().GetDataFromTrainingMaster(); JavaScriptSerializer js = new JavaScriptSerializer(); return js.Serialize(results).ToString(); } 我的.net Web服务返回包含在XML中的JSON,如下所示: [{“Training_Code”:”1234 “,”Training_Duration”:”2hrs “,”Training_Startdate”:”2/14/2013 3:00:00 PM”,”Training_Enddate”:”2/14/2013 5:00:00 PM”,”Trainer_ID”:1,”Training_Location”:”B-Wing Training room-4″,”Comments”:”C# training”,”Keyword”:”C#1234″,”NumberofDays”:1},{“Training_Code”:”4321 “,”Training_Duration”:”16 “,”Training_Startdate”:”2/17/2013 10:30:00 AM”,”Training_Enddate”:”2/17/2013 5:30:00 PM”,”Trainer_ID”:2,”Training_Location”:”A-Wing Training Room-6″,”Comments”:”Objective-C”,”Keyword”:”Obj-C4321″,”NumberofDays”:2}] 我需要它采用以下格式: “Training”:[{“Training_Code”:”1234 “,”Training_Duration”:”2hrs “,”Training_Startdate”:”2/14/2013 3:00:00 PM”,”Training_Enddate”:”2/14/2013 5:00:00 PM”,”Trainer_ID”:1,”Training_Location”:”B-Wing Training room-4″,”Comments”:”C# training”,”Keyword”:”C#1234″,”NumberofDays”:1},{“Training_Code”:”4321 “,”Training_Duration”:”16 “,”Training_Startdate”:”2/17/2013 […]

如何从3.5 asmx Web服务获取JSON响应

我有以下方法: using System.Web.Services; using System.Web.Script.Services; using System.Web.Script.Serialization; using Newtonsoft.Json; using System.Collections; [WebService(Namespace = “http://tempuri.org/”)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //[System.ComponentModel.ToolboxItem(false)] [System.Web.Script.Services.ScriptService] // [System.Web.Script.Services.ScriptService] public class Tripadvisor : System.Web.Services.WebService { public Tripadvisor () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string HotelAvailability(string api) { JavaScriptSerializer js = new JavaScriptSerializer(); […]