javascriptserializer

how to get Json.net serialize DateTime similar to JavaScriptSerializer?

社会主义新天地 提交于 2021-02-16 14:36:10
问题 My code base had been using JavaScriptSerializer for long time, now I am planning to migrate to Json.Net . I saw that DateTime Serializing on both work differently. Console.WriteLine(new JavaScriptSerializer().Serialize(DateTimeOffset.UtcNow)); Output:"\/Date(1403629024695)\/" Console.WriteLine(JsonConvert.SerializeObject(DateTimeOffset.UtcNow)); Output: "2014-06-24T16:57:04.6954145+00:00" Is there a way to get Json.net to serialize similar to JavaScriptSerializer? 回答1: You can use

JSON value is sometimes a string and sometimes an object

孤人 提交于 2021-01-29 03:07:20
问题 I have some JSON that can come in two different formats. Sometimes the location value is a string, and sometimes it is an object. This is a sample of the first format: { "result": [ { "upon_approval": "Proceed to Next Task", "location": "", "expected_start": "" } ] } Class definitions for this: public class Result { public string upon_approval { get; set; } public string location { get; set; } public string expected_start { get; set; } } public class RootObject { public List<Result> result {

Odata Deserialize in json with Enum DisplayName or EnumMember Value

馋奶兔 提交于 2021-01-28 09:07:44
问题 I have an enum declared in c#. Having Display Name/ EnumMember value annotation and Student Class. I'm using asp.net core 2.2+ version with Odata. [DataContract] Public Class Library { public int Id {get;set;} [DataMember(Name="Book_Name")] public BookNameEnum BookName {get; set;} } [DataContract] JsonConverter[typeof(StringEnumConverter)] public enum BookNameEnum { [Display(Name ="John Wick"), EnumMember(value="John Wick")] JohnWick = 1, [Display(Name ="John Snow"), EnumMember(value="John

javascriptserializer date format issue

非 Y 不嫁゛ 提交于 2020-03-22 06:57:43
问题 I am serializing a complex object with lot of properties of other Types and Lists to JSON form but the issue is with DateTime properties. I get the epoch time with JavascriptSerializer (rather than mm/dd/YYYY). Is there any way I can get the datetime in mm/dd/YYYY : HH.MM.SS form without modifying the class defination of the object i am serialzing. 回答1: This cannot be achieved using JavaScriptSerializer and without modifying the underlying class. This could be achieved with Json.Net. 回答2:

Deserialize Json Web API Response - Nested objects

为君一笑 提交于 2020-03-20 06:29:33
问题 I am getting a Web API JSON Response via SSIS Script component, that is coming back in the following format: { "Success": true, "Response": { "Applications": [ { "App_ID": 1638486, "App_Ref": "Test Example", "Status": "Complete", "Error_Code": null, "Error_Message": null, "Create_Dt": "2014-05-14 03:09:01.030 +00:00", "Modify_Dt": "2014-05-14 03:10:59.757 +00:00", "Client_Name": "Silver Chef", "Client_Code": "SLVC01", "Centrelink_Status": "Receiving_Logons" }, { "App_ID": 1637906, "App_Ref":

Deserialize Json Web API Response - Nested objects

删除回忆录丶 提交于 2020-03-20 06:28:23
问题 I am getting a Web API JSON Response via SSIS Script component, that is coming back in the following format: { "Success": true, "Response": { "Applications": [ { "App_ID": 1638486, "App_Ref": "Test Example", "Status": "Complete", "Error_Code": null, "Error_Message": null, "Create_Dt": "2014-05-14 03:09:01.030 +00:00", "Modify_Dt": "2014-05-14 03:10:59.757 +00:00", "Client_Name": "Silver Chef", "Client_Code": "SLVC01", "Centrelink_Status": "Receiving_Logons" }, { "App_ID": 1637906, "App_Ref":

JSON string encoded twice?

徘徊边缘 提交于 2020-03-18 10:17:41
问题 My Web API (code that generates JSON) is generating the following JSON string. It seems, if I am not wrong, that it has been encoded twice: "\"[{\\\"SportID\\\":1,\\\"SportName\\\":\"Tennis\\\"},{\"SportID\\\":2,\\\"SportName\\\":\\\"Footbal\\\"},{\"SportID\\\":3,\"SportName\":\\\"Swimming\\\"}]\"" Web API code: public string JSONTest() { List<Sport> sports = new List<Sport>(); sports.Add(new Sport() { SportID = 1, SportName = "Tennis" }); sports.Add(new Sport() { SportID = 2, SportName =

Json Deserialization is not working with Flurl

六眼飞鱼酱① 提交于 2020-01-06 05:57:31
问题 I try to consume an json APIwith Flurl from a website and I'm getting parse errors or conversion errors. I'm not sure what is my problem since is the first time I deal with json files. I utilize the following online tool to create a C# object out of the json file. But when I try to get a response with Flurl I get the following errors. System.AggregateException: One or more errors occurred. ---> Flurl.Http.FlurlParsingException: Response could not be deserialized to JSON: GET ---> Newtonsoft

C# JavaScriptSerializer and DateTime.MinValue crossing timezones

主宰稳场 提交于 2020-01-05 08:14:10
问题 Given that I have a client and a server, and a class like this: Client side: (TimeZone: UTC -8) class Foo { public int a; public DateTime MyTime { get; set; } } var serialized = new JavaScriptSerializer().Serialize(new Foo { a = 1 }); // Send serialized to server Server side: (TimeZone UTC-4) // Receive JSON var deserialized = new JavaScriptSerializer().Deserialize<Foo>(serialized); if (deserialized.MyTime == DateTime.MinValue) { // dostuff } My problem here is that JavaScriptSerializer

Validating JSON before deserializing into C# object

我的梦境 提交于 2020-01-03 03:59:06
问题 If a JavaScript object in JSON is not going to deserialize into the C# object I want, how can I interrogate it to provide an error message explaining what is wrong with the input? (Assuming the JSON format is correct, just the data is not valid.) My C# class: (simplified) public class Dependent { public Dependent() { } public string FirstName { get; set; } public DateTime DateOfBirth { get; set; } } Test code to deserialize: string dependents = @"[ { ""FirstName"": ""Kenneth"", ""DateOfBirth"