json.net

Deserialize JSON with json.NET into C# dynamic

自古美人都是妖i 提交于 2019-12-04 05:27:59
I have following problem: I have a json file that looks like this { "Path": { "FirstPath": "/1/2/text()" } } If I parse this JSON-File with Newtonsoft like this dynamic dyn = JObject.Parse(json); or this dynamic dyn = JsonConvert.DeserializeObject(json); I get a dynamic object that needs to be used like this dyn.Path.FirstPath.Value How can I get rid of the Value stuff? All of my objects in the JSON end up being a string. I don't want to always write ".Value" at the end if it is not necessary. I tested this using Newtonsoft 8.0.2 and it works fine. dynamic dyn = JObject.Parse(json); string

Web API: Failed to serialize the response body for content type

不羁岁月 提交于 2019-12-04 05:26:06
问题 I am working with ASP.NET MVC 5 Web Api. I am having existing application with many api's. Recently I have implemented custom JsonConverter which will convert Date as per timezone. public class CustomInfoConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(CustomType); } public override bool CanRead { get { return false; } } public override bool CanWrite { get { return true; } } public override void WriteJson(JsonWriter writer, object

How to set Json.NET ContractSerializer for a certain specific type instead of globally?

孤街浪徒 提交于 2019-12-04 05:10:42
I want to set a contract serializer just for certain types in my ASP.NET Web API application. I can set the settings globally in the App_Start/FormatterConfig.cs like this: public static void RegisterGlobalFormatters(MediaTypeFormatterCollection formatters) { jsonSerializerSettings.ContractResolver = new CriteriaContractResolver(new List<string>(new string[]{"mdData", "name", "label"})); ... but how can I just apply this to one or more specific class types? The reason I want to do this is because I need to be able to set what fields should be serialized at runtime based on configuration or

Json.Net Resolving Property Names Properly

坚强是说给别人听的谎言 提交于 2019-12-04 05:01:00
问题 I am getting some data that looks like below JSON from an API { body_html: "<h1>Test</h1>", id: "cu1bpkz", link_id: "d3_3kkgis", author: "jdoe", author_flair_text: null, author_flair_css_class: null, parent_id: "t3_3kkgis", body: "Test", subreddit_id: "q5_39vkz", created_utc: 1442108087, subreddit: "test" } And here is my Strongly-typed class that I use for deserialization: public class Comment { public string AuthorFlairText { get; set; } public string AuthorFlairCssClass { get; set; }

Adding object to JArray overwriting first element

佐手、 提交于 2019-12-04 05:01:00
问题 When adding a new object to a JArray each object is added to the first entry in the array ([0]) along with being appended to the array. response = client.GetAsync(new Uri(urlIndexDoc)).Result; result = response.Content.ReadAsStringAsync().Result; JObject OPDDoc = JObject.Parse(result); JArray indexCEM = new JArray(); JObject oNew = new JObject(); int idxcount = Convert.ToInt32(ConfigurationManager.AppSettings["IndexCount"]) + 1; for (int i = 1; i < idxcount ; i++) { string istring = i

Refactor of ShouldSerialize () in class… can I use IContractResolver?

ε祈祈猫儿з 提交于 2019-12-04 04:57:21
问题 I have an API that returns a big list of car features.... all are either bool or ints... and basically I only want to display the ones that return true values or >0 for the ints. I am using JSON.net so that I san use the ShouldSerialize() property to determine if I should serialize the property based upon its value and my code looks like this: public class Features { public bool ABS { get; set; } public bool ShouldSerializeABS() { // don't serialize the ABS property if ABS is false return

JsonConverter Attribute : deserialize using custom constructor and Autofac

前提是你 提交于 2019-12-04 04:56:12
问题 Am using a custom JsonConverter to convert my JSON object. This is achieved via the JsonConverter attribute to the IQuery object below [JsonConverter(typeof(CustomConverter<IQuery>))] public interface IQuery { } The custom generic class is below (some bits removed for brevity) public class CustomConverter<T> : JsonConverter { // This should be created via AutoFac public ICustomObjectCreator<T> ObjectCreator { get; set; } // This default constructr always gets called public CustomConverter() {

JSON.NET Why does it Add to List instead of Overwriting?

别来无恙 提交于 2019-12-04 04:40:06
问题 public class MyClass { public List<int> myList = new List<int> { 1337 }; public MyClass() {} } var myClass = JsonConvert.DeserializeObject<MyClass>("{myList:[1,2,3]}"); Console.WriteLine(string.Join(",", myClass.myList.ToArray())); //1337,1,2,3 Why does it display 1337,1,2,3 instead of 1,2,3? Is there a way/setting to make JSON.NET overwrite List instead of adding elements to it? I need a solution that doesn't modify the constructor. 回答1: If you don't want this behavior, you can change it by

Getting SerializeObject to use JsonProperty “name” defined inside interface

我的梦境 提交于 2019-12-04 04:16:59
问题 When calling "JsonConvert.SerializeObject" I am passing in an object that implements an interface. It is the interface that defines the JsonProperty attributes to set the desired JSON object property name. However when I examine the JSON object that is produced it is using the actual .NET property name, rather than JsonPropertyAttribute value. This leads me to believe it is only reflecting over the implementation of the interface to find the JsonProperty attributes, rather than the interface

Insert a single object into json file without rewriting entire file

被刻印的时光 ゝ 提交于 2019-12-04 04:16:37
问题 I'm working on a method that uses JSON.NET to add a horse object to a JSON-formatted database of horses. One option is to deserialize the entire file into a list of horses, add the new horse, then serialize the list and rewrite the whole file. I've implemented this approach in the code below. // adds a horse to the db public int AddHorse(Horse horse) { // identify and assign next available id to horse var horses = GetAllHorses(); int nextId = horses.Max(h => h.ID) + 1; horse.ID = nextId; //