json.net

How to return json date from MVC4 controller in ISO format

跟風遠走 提交于 2019-12-07 04:05:30
I tried tro return date in ISO format using Json.Net from ASP.NET MVC4 controller public JsonResult Sales() { var saleList = new List<Sale>(); ... var str = JsonConvert.SerializeObject(saleList); return Json(str, JsonRequestBehavior.AllowGet); } public class Sale { public DateTime saledate { get; set; } ... } But it returns whole object json notation as single string. How to return date in ISO format as json object ? You can do it with ServiceStack JSON serializer but first you have to integrate it to ASP.NET MVC. After installing the package, configure DateTime serialization in application

How to deserialize JSON array with “root” element for each object in array using Json.NET?

左心房为你撑大大i 提交于 2019-12-07 04:02:51
问题 I have following JSON string: [ { "Person" : { "Name" : "John", "Gender" : "male" } }, { "Person" : { "Name" : "John", "Gender" : "male" } } ] (As you may notice unfortunately I have a sort of "root" element for each object in the array. Without this "root" element the task becomes quite trivial.) I have to deserialize it into a list of Person class: class Person { public string Name { get; set; } public string Gender { get; set; } } ... List<Person> ListPersons() { return JsonConvert

JSON.NET JToken Keys Are Case Sensitive?

为君一笑 提交于 2019-12-07 03:32:23
问题 I'm having to perform some custom deserialization with JSON.NET and I just found that it's treating the key values in a JToken as case sensitive. Here's some code: public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); JToken version = token["version"]; string ver = version.ToObject<string>(); return new MyVersion(ver); } The version variable is null even though the json contains a version

Web API project won't run when its deployed - Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0

不想你离开。 提交于 2019-12-07 03:26:41
问题 I keep getting this error when I deploy my MVC 5 WEB API project: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. I have followed this and re-install the NuGet package "Update-Package Newtonsoft.Json -Reinstall" but it didn't work. Does anyone have any idea here as to what could be going wrong? 回答1: Check the reference to the Newtonsoft.json DLL and make

Json.net getter property not serialized

眉间皱痕 提交于 2019-12-07 03:23:55
问题 I've started using json.net to produce better DateTimes, but I've noticed that one of my properties isn't being serialized. It has no setter, and its getter is reliant upon another member of the object, e.g. public int AmountInPence { get; set;} public decimal AmountInPounds { get { return (decimal)AmountInPence / 100; } } I've made a class that inherits from JsonResult and the main line is: string serializedObject = JsonConvert.SerializeObject(Data, new IsoDateTimeConverter()); Can anyone

Get the name of a JObject in Json.Net

僤鯓⒐⒋嵵緔 提交于 2019-12-07 03:09:29
问题 I have a JObject equal to: "Info": { "View":"A", "Product":"B", "Offer":"Offer1", "Demo":"body {background-color:red;} #box {border:dotted 50px red;}", "Log":false } How can I return the name of the object, "Info"? I am currently using the Path property like so: jObject.Name = jObject.Path.Substring(jObject.Path.jObject('.') + 1); Is there a better way to do this? 回答1: In JSON, objects themselves do not have names. An object is just a container for a collection of name-value pairs, beginning

Setting a custom json converter for DocumentDb

杀马特。学长 韩版系。学妹 提交于 2019-12-07 02:57:51
问题 I am using a typed DocumentQuery to read documents from a collection of an Azure DocumentDb. from f in client.CreateDocumentQuery<MyModel>(Collection.SelfLink) select f Because I do not find a way how I can set the neccesarry custom json converter, it throws this exeption: Could not create an instance of type AbstractObject. Type is an interface or abstract class and cannot be instantiated. Usually you do something like this to make it work: var settings = new JsonSerializerSettings();

How do I serialize IHtmlString to JSON with Json.NET?

本秂侑毒 提交于 2019-12-07 02:50:39
问题 I have a field containing raw HTML published via JSON that was recently converted from a string to an IHtmlString. When that change happened, the field went from being a JSON string to being an empty object and a bunch of things consuming that JSON started blowing up. // When it was a string... { someField: "some <span>html</span> string" } // When it became an IHtmlString... { someField: { } } Ignoring any arguments against raw HTML in JSON since it is moot for this project, how do I get the

How to get first key from JObject?

强颜欢笑 提交于 2019-12-07 02:30:32
问题 I am using Newtonsoft.Json in my project. I have JObject like this: { "4781":"Name 1", "1577":"Name 2", "9973":"Name 3" } I successfully parse it with JObject.Parse() . I need to get first key from this JObject ("4781"). How do I get it? 回答1: Json.NET doesn't directly provide integer indexed access to the properties of a JObject. If you do JObject.Parse(jsonString)[0] you get an ArgumentException with the message Accessed JObject values with invalid key value: 0. Object property name expected

Improve performance of XmlSerializer

我们两清 提交于 2019-12-07 02:19:59
问题 I use a XmlSerializer to serialize/deserialize some objects. The problem is the performance. When profiling, using the XmlSerializer make our application 2 seconds longer to start. We cache our XmlSerializer and reuse them. We cannot use sgen.exe because we are creating the XmlSerializer with XmlAttributeOverrides . I try to use serialization alternative like Json.Net and, at first, it's work great. The problem is that we need to be backward compatible so all the xml already generated need to