json.net

Json Datetime issue [duplicate]

心不动则不痛 提交于 2019-12-10 00:52:37
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: how to force netwtonsoft json serializer to serialize datetime property to string? I am using Newtonsoft.Json to convert my object in to JSON file. But I am having issue with the DateTime , in my object datetime field is set as "7/30/2012 8:29:12 PM" but in JSON file. I am getting DateTime field in this format: "\/Date(1343660352227+0530)\/" . I need to get the DateTime in the same format as that of the object.

What is the existingValue parameter used for in the ReadJson method of a JsonConverter?

北慕城南 提交于 2019-12-10 00:36:37
问题 When creating a custom Json Converter one of the methods which needs to be overridden is: public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) What is "existingValue" parameter used for? What does the variable name "existingValue" mean in this context? 回答1: Simply put, the existingValue parameter gives you the existing or default value of the object that will ultimately be replaced with the value returned from the ReadJson method

VB.NET Deserialize JSON to anonymous object using newtonsoft returned error

随声附和 提交于 2019-12-09 23:22:19
问题 I would like to deserialize the returned JSON from a service call in VB.NET to an anonymous type but I was having error. It works in C# using dynamic type but i dont know how to do it in VB. Here is my JSON returned from a web service call: {"format":"png","height":564,"width":864} Here is my VB code json above assigned to param text: Dim testObj = Newtonsoft.Json.JsonConvert.DeserializeObject(text) But when i tried to access testObj.format, an exception was thrown with message {"Public

Convert DataSet with multiple Datatables to Json

余生颓废 提交于 2019-12-09 19:05:19
问题 I want to convert a dataset which has multiple data-tables within it. Following is the example, The dataset X has two data tables A and B I want the result as follows, { "type":"A", "value":"100", "details":[ {"name":"John", "age":"45", "gender":"M"}, {"name":"Sebastin", "age":"34", "gender":"M"}, {"name":"Marc", "age":"23", "gender":"M"}, {"name":"Natalia", "age":"34", "gender":"F"} ] } Currently I am using Newtonsoft.Json. Is it possible with Newtonsoft.Json? If not, is it possible with any

JSON.NET: Deserializing part of a JSON object to a dictionary

▼魔方 西西 提交于 2019-12-09 18:27:47
问题 I have JSON like this: { "Property":"Blah blah", "Dictionary": { "Key1" : "Value1", "Key2" : "Value2", "Key3" : "Value3" } } I want to extract the "Dictionary" object as a Dictionary (so it'd be like Key1 => Value1, etc.). If I just had the "Dictionary" object directly, I could use: JsonConvert.DeserializeObject<Dictionary<string, string>> What's the best way to get just the Dictionary property as a Dictionary? Thanks in advance! Tim 回答1: Took me a little while to figure out, but I just didn

Serialize subclass with Json.NET

时间秒杀一切 提交于 2019-12-09 16:39:41
问题 I am trying to use Json.NET to serialize a subclass. The resulting json contains the serialized properties for the superclass but not the properties on the subclass object. This seems to be related to an issue I found here on SO. But having to write a JsonConverter seems like overkill. Sample subclass: public class MySubclass : List<string> { public string Name { get; set; } } Sample of the serialization: MySubclass myType = new MySubclass() { Name = "Awesome Subclass" }; myType.Add("I am an

Custom Json.NET contract resolver for lowercase underscore to CamelCase

主宰稳场 提交于 2019-12-09 15:12:21
问题 I'm working on a REST API in ASP.NET MVC where the resulting serialised JSON uses lowercase_underscore for attributes. From a class Person with string properties FirstName and Surname , I get JSON as follows: { first_name: "Charlie", surname: "Brown" } Note the lowercase_underscore names. The contract resolver I use to do this conversion automatically for me is: public class JsonLowerCaseUnderscoreContractResolver : DefaultContractResolver { private Regex regex = new Regex("(?!(^[A-Z]))([A-Z]

Newtonsoft.Json customize serialize dates

被刻印的时光 ゝ 提交于 2019-12-09 14:43:58
问题 I am using Newtonsoft.Json for serializing my dates from C# to javscript what I want to do is have the json serializer use the current culture for formatting dates into string Here is what I am getting by my Json serializer: JsonConvert.SerializeObject(DateTime.Now); result is: "2016-07-08T17:10:17.6722753-05:00" But what I prefer is: "08/07/2016 17:10:57" Because my current culture is brazil and I want my dates to be displayed the above way. Is it possible to Globally ( for any date that may

Why ServiceStack.Text doesn't default dates to iso8601?

纵饮孤独 提交于 2019-12-09 14:05:34
问题 If I use Newtonsoft.Json.NET it defaults to iso8601 (i.e.: 2011-06-02T09:34:29+02:00 ) for serializing/deserializing dates. Why ServiceStack.Text doesn't default to this and I need to specify it as a configuration setting? 回答1: ServiceStack followed the .NET DataContractSerializer defaults, not JSON.NET. We are reluctant to make breaking changes like this, especially when there's an easy way to configure it otherwise with: JsConfig.DateHandler = DateHandler.ISO8601; 来源: https://stackoverflow

Retrieving “images” from a JSON string retrived from Instagram API

喜你入骨 提交于 2019-12-09 13:55:07
问题 Using C# and Visual Studio 2010 (Windows Form Project), InstaSharp and Newtonsoft.Json libraries. I want to get the image url from the JSON string returned to me by the Endpoint Instagram API when I request for a particular hashtag. I can so far retrive the JSON string. I am trying to use Newtonsoft.Json to deserialize the object using the examples, but I probably dont understand the JSON string representation of the object properly. Below is a simplified sample response I get from the api