javascriptserializer

JavaScriptSerializer UTC DateTime issues

谁说胖子不能爱 提交于 2019-11-26 22:46:11
Our client wanted to show the date and time values in the browser exactly as they are in the database, and we are storing them as UTC in the database. At first we had some problems with the serialization and Javascript side. The DateTime values got shifted twice - at first to match the local time zone of the machine and then to match the time zone in the browser. We fixed it by adding a custom Converter to the JavaScriptSerializer. We marked the DateTime to be of DateTimeKind.Utc in the Serialize override. It was a bit hard to feed the data back from the Serialize but we found some Uri hack

From DataTable in C# .NET to JSON

和自甴很熟 提交于 2019-11-26 19:20:51
问题 I am pretty new at C# and .NET, but I've made this code to call a stored procedure, and I then want to take the returned DataTable and convert it to JSON. SqlConnection con = new SqlConnection("connection string here"); SqlDataAdapter da = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand("getDates", con); SqlParameter par = new SqlParameter("@PlaceID", SqlDbType.Int); par.Value = 42; da.SelectCommand = cmd; cmd.Parameters.Add(par); DataSet ds = new DataSet(); DataTable dt = new DataTable

Return JSON from ASMX web service, without XML wrapper?

Deadly 提交于 2019-11-26 19:09:00
I need to get Json data from a C# web service. I know there are several questions based on this, trust me I have read through quite a few but only to confuse me further. This is what I have done : In my web service I have included : [System.Web.Script.Services.ScriptService] for the class & [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] for the method I have also used a JavaScriptSerializer() to convert my data to a string I am calling this service using $.getJSON() If I don't use that I get an Cross domain reference error. To do this I had to setup m service to get

How to get JSON response from a 3.5 asmx web service

拈花ヽ惹草 提交于 2019-11-26 18:59:10
I have the following method: 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(); }

JavaScriptSerializer - custom property name

霸气de小男生 提交于 2019-11-26 18:32:16
问题 I am using JavaScriptSerializer to deserialize json data. Everything works pretty well, but my problem is, that one property in json data is named 'base', so I cannot create such property in my C# code. I found that i can manually map values to properties in constructor, but the issue is, that my DTOs have like 200 properties, so I do not want to make this manually and would prefer to find any other solution. I also Tried to use annotations, but this: [JsonProperty("base")] public int

Serializing dictionaries with JavaScriptSerializer

落花浮王杯 提交于 2019-11-26 17:14:00
问题 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"} )? 回答1: 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

How to set formatting with JavaScriptSerializer when JSON serializing?

风流意气都作罢 提交于 2019-11-26 16:33:02
问题 I am using JavaScriptSerializer for serializing objects to the file to the JSON format. But the result file has no readable formatting. How can I allow formating to get a readable file? 回答1: It seemed to be that there is no built-in tool for formatting JSON-serializer's output. I suppose the reason why this happened is minimizing of data that we send via network. Are you sure that you need formatted data in code? Or you want to analize JSON just during debugging? There is a lot of online

Deserializing a JSON file with JavaScriptSerializer()

假装没事ソ 提交于 2019-11-26 14:28:46
问题 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

Can JavaScriptSerializer exclude properties with null/default values?

倖福魔咒の 提交于 2019-11-26 13:05:24
问题 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. 回答1: 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 :

How to decode a JSON string using C#?

我们两清 提交于 2019-11-26 12:45:03
问题 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 = ?? 回答1: 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