json.net

Looping through generic object properties

不羁岁月 提交于 2019-12-08 05:44:19
问题 I have a JSON string stored in a database like this: { "Error": "Lol", "ErrorDate": "2016-05-13T10:43:27.6365795Z", "Application": "Business", "UserName": "Josh", "TraceSession": "moo" } The JSON string is different for each item in the table. In my application I want to deserialize this into an object, but because the JSON is always different I can't create a model. Currently I have this: var jObject = JsonConvert.DeserializeObject(FieldNames); which I thought would work, but now I need to

How to Serialize a Dictionary<string,string> and not surround the value with quotes?

回眸只為那壹抹淺笑 提交于 2019-12-08 05:30:12
问题 Example model: public class Thing { [JsonProperty("foo")] public string Foo {get;set;} [JsonProperty("bars")] public Dictionary<string,string> Bars {get;set;} } i want the output to look like this: {"foo":"Foo Value", "bars":{"key1":key1Value,"key2":key2Value}} The reason I want the values of the Dictionary to be without quotes is so I can pull the value from the client via jquery: {"foo":"Foo Value", "bars":{"key1":$('#key1').val(),"key2":$('#key2').val()}} Is this possible using Json.Net?

Json.NET: Deserialization with list of objects

99封情书 提交于 2019-12-08 05:10:02
问题 I am processing JSON responses (displayed below) from a service on my Windows Phone 7 client. I am using Json.NET to deserialize them into an object, which contains List of products. But after I deserialize, when I looked into my serviceresponse object, I can see a list of 2 products. But when I expand the product object, the fields under product (Name, ExpiryDate... etc ) are all null. I guess my problem is with the way I have defined my serviceresponse class. Can someone help me to resolve

JsonSerializationException for type with private constructor

放肆的年华 提交于 2019-12-08 04:11:24
问题 I am persisting NLog logging statements to my RavenDb database. The LogEventInfo class, which represents a log statement, has a property, LogLevel , with a private constructor. Instances of LogLevel (Info, Warn, etc.) are created via static, readonly properties that call the private constructor. The problem is that I wish to read the messages out of the database and querying for them is throwing a Json.Net serialization error: Unable to find a constructor to use for type NLog.LogLevel. A

Removing NaN values in deserialization Web API 2 C#

南笙酒味 提交于 2019-12-08 03:56:42
问题 Hello I am wondering if anyone could help me, I am trying to automatically replace NaN from double values automatically with 0 upon deserializing automatically in Web API 2. I am trying to use JSON.NET but I am having no success. Any help would be greatly appreciated. I am placing the below into my WebApiConfig config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); MediaTypeHeaderValue appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes

Newtonsoft JsonConvert.DeserializeObject how to ignore empty objects

老子叫甜甜 提交于 2019-12-08 03:20:45
问题 I have the following class definitions: public class Tag { public Guid? TagId { get; set; } public string TagText { get; set; } public DateTime CreatedOn { get; set; } } public class Wiki { public Guid? WikiId { get; set; } public string WikiText { get; set; } public string Title { get; set; } public DateTime CreatedOn { get; set; } public IEnumerable<Tag> Tags { get; set; } } From the database i get the following json Object: { "WikiId": "83981284-0AD3-4420-90AB-15E3BF6BD7B7", "WikiText":

Is it possible to specify an offset for JSON.NET deserialization of DateTimeOffset?

萝らか妹 提交于 2019-12-08 03:04:00
问题 The following statement prints "1/1/0001 4:00:00 PM -05:00" Console.WriteLine(JsonConvert.DeserializeObject<DateTimeOffset>("\"0001-01-01T16:00:00\"")); This is because when json.net deserializes a DateTime string (which doesn't have an offset) to a DateTimeOffset object, it assigns the local offset, which in this case is -05:00. What if I don't want to use the local offset? Is there any way I can specify an offset to use for this deserialization? (The use case is the database server and the

How to get ReadJson to return “default” behavior - as if CanConvert returned false

夙愿已清 提交于 2019-12-08 02:56:31
问题 I have created an implementation of JsonConverter CanConvert always returns true. In ReadJson I want to sometimes just use the "default" behavior, as if CanConvert had returned false and my ReadJson was never called. Various other posts have suggested I do some variation of the following: existingValue = existingValue ?? serializer .ContractResolver .ResolveContract(objectType) .DefaultCreator(); serializer.Populate(reader, existingValue); However, this throws NullReferenceException on

Making custom JsonConverter respect ItemTypeNameHandling when serializing with JSON.NET

泄露秘密 提交于 2019-12-08 02:49:49
问题 I have an object with a list of base class sub-objects. Sub-objects need a custom converter. I can't make my custom converter respect ItemTypeNameHandling option. Sample code (create a new C# Console project, add JSON.NET NuGet package): using System; using System.Collections.Generic; using Newtonsoft.Json; namespace My { class Program { private static void Main () { Console.WriteLine(JsonConvert.SerializeObject( new Box { toys = { new Spintop(), new Ball() } }, Formatting.Indented)); Console

Reading dynamic attributes of json into .net C#

烂漫一生 提交于 2019-12-08 02:38:12
问题 I am getting following json format after hitting to an API: { "7407": { "survey_id": "406", "device_id": "1", "response_time": "2013-10-10 16:14:01", "timezone": "0", "language_id": "en", "response_id": "7407", "device_alias": "QR Code App", "site_name": "QR Code App", "country_name": "United States", "state_name": "New York", "city_name": "Suffern", "zip": "", "voucher_name": null, "voucher_mode": null, "surveyee_name": null, "surveyee_email": null, "surveyee_phone": null, "ques": { "": [] }