javascriptserializer

Serializing dictionaries with JavaScriptSerializer

六眼飞鱼酱① 提交于 2019-11-27 15:35:37
Apparently, IDictionary<string,object> is serialized as an array of KeyValuePair objects (e.g., [{Key:"foo", Value:"bar"}, ...] ). Is is possible to serialize it as an object instead (e.g., {foo:"bar"} )? 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 Deserialize(IDictionary<string,

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

你说的曾经没有我的故事 提交于 2019-11-27 14:25:39
I'm not sure where I'm going wrong of what I'm missing. I'm building an ASP.NET 2.0 (on the .Net 3.5 framework) Web application and I am including a webservice. Note that this is not an MVC project. I wish to expose a method which will return a JSON string; formatted to feed the jqGrid jQuery plugin. This is the preliminary test method I've implemented in my service: thanks to ( Phil Haack's Guide for MVC ) [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string getData() { JavaScriptSerializer ser = new JavaScriptSerializer(); var jsonData = new { total = 1, // we'll

Deserializing a JSON file with JavaScriptSerializer()

蓝咒 提交于 2019-11-27 09:00:48
the json file's structure which I will deserialize looks like below; { "id" : "1lad07", "text" : "test", "url" : "http:\/\/twitpic.com\/1lacuz", "width" : 220, "height" : 84, "size" : 8722, "type" : "png", "timestamp" : "Wed, 05 May 2010 16:11:48 +0000", "user" : { "id" : 12345, "screen_name" : "twitpicuser" } } I have created a class which has the filed names as properties for JavaScriptSerializer. The code which I will use to Deserialize the json is as follows; using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) { var responseBody = reader.ReadToEnd(); var deserializer

JavaScriptSerializer not deserializing DateTime/TimeSpan Properly

懵懂的女人 提交于 2019-11-27 07:28:40
问题 Having a problem where DateTime/TimeSpan doesn't seem to deserialize properly with JavaScriptSerializer. When I get the Object back after deserializing the TimeSpan is empty and if I use DateTime then the times are all out of whack. Did find this article but it didn't really help me too much. http://www.west-wind.com/weblog/ShowPost.aspx?id=471402 Anyone have any ideas? Should I maybe try the json.net library? public class JsonFilter : ActionFilterAttribute { public string Param { get; set; }

Can JavaScriptSerializer exclude properties with null/default values?

眉间皱痕 提交于 2019-11-27 07:13:32
I'm using JavaScriptSerializer to serialize some entity objects. The problem is, many of the public properties contain null or default values. Is there any way to make JavaScriptSerializer exclude properties with null or default values? I would like the resulting JSON to be less verbose. Mitch 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

How to decode a JSON string using C#?

回眸只為那壹抹淺笑 提交于 2019-11-27 04:34:15
I'm looking for an example code/lib to decode a JSON string using C#. To encode I can do this: var data = new Dictionary<string,string>(); data.Add("..", "..."); var json_encoded = new JavaScriptSerializer().Serialize(data); but how do I decode? var json_decoded = ?? 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 This

JavaScriptSerializer.MaxJsonLength exceeded. What's the best practice for handling this?

为君一笑 提交于 2019-11-27 03:51:46
问题 I've got a large amound of data I'm sending down to the client using jQuery's $.ajax() function. I'm calling a method in a ASP.NET web service that returns the JSON data. Everything is great for most searches, but when I've got a large data set to return I run into a issue with the JavaScriptSerializer MaxJsonLength property. What's the best practice for handling this? I don't want to just arbitrarily set a max length. Can I set the MaxJsonLength in the web service if the data being returned

JavaScriptSerializer is subtracting one day from date

给你一囗甜甜゛ 提交于 2019-11-27 03:07:58
问题 I am using JavaScriptSerializer for serializing DateTime, but when I deserialize it show one day less from the date it get serialize: Here is test: DateTime startDate=new DateTime(2012,1,20);//set the 20th of January JavaScriptSerializer serializer=new JavaScriptSerializer(); string serializeDate= serializer.Serialize(startDate); DateTime afterDeserialize= serializer.Deserialize<DateTime>(serializeDate);//I get 19th of Jan Assert.Equals(startDate, afterDeserialize); firstly I thougt it

JavaScriptSerializer with custom Type

六眼飞鱼酱① 提交于 2019-11-27 02:47:28
问题 I have a function with a List return type. I'm using this in a JSON-enabled WebService like: [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<Product> GetProducts(string dummy) /* without a parameter, it will not go through */ { return new x.GetProducts(); } this returns: {"d":[{"__type":"Product","Id":"2316","Name":"Big Something ","Price":"3000","Quantity":"5"}]} I need to use this code in a simple aspx file too, so I created a

Asmx web service how to return JSON and not XML?

独自空忆成欢 提交于 2019-11-27 02:18:12
My service method: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string getDataFromTrainingMaster() { List<TrainingMasterDataStruct> results = new DAL().GetDataFromTrainingMaster(); JavaScriptSerializer js = new JavaScriptSerializer(); return js.Serialize(results).ToString(); } My .net web service returns JSON wrapped in XML as follows: <?xml version="1.0" encoding="UTF-8"?> <string xmlns="http://tempuri.org/"> [{"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,