Serializing dictionaries with JavaScriptSerializer

Although I agree that JavaScriptSerializer is a crap and Json.Net is a better option, there is a way in which you can make JavaScriptSerializer serialize the way you want to. You will have to register a converter and override the Serialize method using something like this: public class KeyValuePairJsonConverter : JavaScriptConverter { public override object … Read more

ASP.NET WebService is Wrapping my JSON response with XML tags

In your code, don’t “return” the json. Use instead: Context.Response.Write(ser.Serialize(jsonData)); Then you’ll be good. The regular return command helps you by putting in a more proper service format. Some would say it’d be better form to use this, and unwrap your json on the client from this format. I say, just spit down the stuff … Read more

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

Asmx web service how to return JSON and not XML?

There’s no need to create the JSON string yourself. Return the List<TrainingMasterDataStruct> and let .NET serialise it for you on the fly: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<TrainingMasterDataStruct> getDataFromTrainingMaster() { List<TrainingMasterDataStruct> list = doStuff(); return list; } Secondly, the non-static method signatures suggest this is an asmx file (page methods are static). By default these … Read more

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

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

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