Can JavaScriptSerializer exclude properties with null/default values?

FYI, if you’d like to go with the easier solution, here’s what I used to accomplish this using a JavaScriptConverter implementation with the JavaScriptSerializer: private class NullPropertiesConverter: JavaScriptConverter { public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) { throw new NotImplementedException(); } public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) { var jsonExample … Read more

How to decode a JSON string using C#?

You can do this: var data = new Dictionary<string, string>(); data.Add(“foo”, “baa”); JavaScriptSerializer ser = new JavaScriptSerializer(); var JSONString = ser.Serialize(data); //JSON encoded var JSONObj = ser.Deserialize<Dictionary<string, string>>(JSONString); //JSON decoded Console.Write(JSONObj[“foo”]); //prints: baa

JavaScriptSerializer UTC DateTime issues

JavaScriptSerializer, and DataContractJsonSerializer are riddled with bugs. Use json.net instead. Even Microsoft has made this switch in ASP.Net MVC4 and other recent projects. The /Date(286769410010)/ format is proprietary and made up by Microsoft. It has problems, and is not widely supported. You should use the 1979-02-02T02:10:10Z format everywhere. This is defined in ISO8601 and RF3339. … Read more

How to get JSON response from a 3.5 asmx web service

I faced the same issue, and included the below code to get it work. [WebMethod] [ScriptMethod(UseHttpGet=true ,ResponseFormat = ResponseFormat.Json)] public void HelloWorld() { Context.Response.Clear(); Context.Response.ContentType = “application/json”; Context.Response.Write(“Hello World”); //return “Hello World”; } Update: To get a pure json format, you can use javascript serializer like below. public class WebService1 : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet=true … Read more

Return JSON from ASMX web service, without XML wrapper?

Use this: var JsonString = ….; $.ajax({ type: “POST”, contentType: “application/json; charset=utf-8”, url: “YourWebServiceName.asmx/yourmethodname”, data: “{‘TheData’:'” + JsonString + “‘}”, dataType: “json”, success: function (msg) { var data = msg.hasOwnProperty(“d”) ? msg.d : msg; OnSucessCallBack(data); }, error: function (xhr, status, error) { alert(xhr.statusText); } }); function OnSuccessCallData(DataFromServer) { // your handler for success } and … Read more

JavaScriptSerializer.Deserialize – how to change field names

I took another try at it, using the DataContractJsonSerializer class. This solves it: The code looks like this: using System.Runtime.Serialization; [DataContract] public class DataObject { [DataMember(Name = “user_id”)] public int UserId { get; set; } [DataMember(Name = “detail_level”)] public string DetailLevel { get; set; } } And the test is: using System.Runtime.Serialization.Json; [TestMethod] public void … Read more

How do I get formatted JSON in .NET using C#?

You are going to have a hard time accomplishing this with JavaScriptSerializer. Try JSON.Net. With minor modifications from JSON.Net example using System; using Newtonsoft.Json; namespace JsonPrettyPrint { internal class Program { private static void Main(string[] args) { Product product = new Product { Name = “Apple”, Expiry = new DateTime(2008, 12, 28), Price = 3.99M, … Read more

JavaScriptSerializer – JSON serialization of enum as string

I have found that Json.NET provides the exact functionality I’m looking for with a StringEnumConverter attribute: using Newtonsoft.Json; using Newtonsoft.Json.Converters; [JsonConverter(typeof(StringEnumConverter))] public Gender Gender { get; set; } More details at available on StringEnumConverter documentation. There are other places to configure this converter more globally: enum itself if you want enum always be serialized/deserialized as … Read more