json.net

Is this possible in JSON?

自作多情 提交于 2019-12-10 13:54:05
问题 I am currently working with json.net ! I know how to deserialize json data and how to map with our class. Now I am eager about some queries ! Suppose my jsonstrings is "attributes":{ "color":"Brown", "condition":"Used", "currency":"USD", "price":"10500", "price_display":"$10,500", } and my attribute class ( in which i want to deserialize above string ) is Public class Attribute { public string name{ get; set; } public string value{ get; set; } } Now I am using following code for

How to cleanly deserialize JSON where string values are wrapped in objects of the same name

岁酱吖の 提交于 2019-12-10 13:37:24
问题 I want to deserialize some strange JSON to C# classes: { "Result": { "Client": { "ProductList": { "Product": [ { "Name": { "Name": "Car polish" } } ] }, "Name": { "Name": "Mr. Clouseau" }, "AddressLine1": { "AddressLine1": "Hightstreet 13" } } } } json2csharp generates the following classes for the JSON: public class Name { public string Name { get; set; } } public class Product { public Name Name { get; set; } } public class ProductList { public List<Product> Product { get; set; } } public

Deserialize JSON object property to string

五迷三道 提交于 2019-12-10 13:20:40
问题 I have some JSON that has a variety of properties most of which are simple data types. However, I have one property in the JSON that when I deserialize it to a C# class I simply need it to be deserialized as a string. Example JSON: {"simpleProperty": "value1", "json":{"a":"a1", "b":"b1"}} The "json" object has no set structure other than it will be a valid JSON object. So in the above example the value of "json" is a JSON object -- but when it gets deserialized, I need it as a string. So if

DateTime.TryParse cannot parse DateTime.MinValue

倾然丶 夕夏残阳落幕 提交于 2019-12-10 13:13:33
问题 I'm using a library called Json.NET that uses the following code internally to parse a JSON string into a DateTime: if (DateTime.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt)) { dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling); SetToken(JsonToken.Date, dt); return dt; } I thought Json.NET was screwing up the conversion, but it looks like it's DateTime.TryParse itself that's botching the value. When I parse the following valid Iso date (which corresponds to UTC DateTime

How to convert any pascal case JSON object to camel case JSON object?

非 Y 不嫁゛ 提交于 2019-12-10 12:58:41
问题 I tried using CamelCasePropertyNamesContractResolver, however it does not convert pascal property names into camel casing? Note: this is an example only, my json input is unknown, I only the json pascal casing. using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; namespace Example { class Program { static void Main(string[] args) { object myJsonInput = @"{'Id':'123','Name':'abc'}"; //Example only, any json. object myJsonOutput; var jsonSerializersettings = new

Validating result of JsonConvert.DeserializeObject (think “try parse”) using JSON.Net

痞子三分冷 提交于 2019-12-10 12:58:04
问题 I have incoming messages that I need to try and parse in my own objects structure. SOme of these are well formed JSON obejcts and some are just nonsense. I use JsonConvert.DeserializeObject<MyObject>(incmoingString); to do this. This however sometimes gives me a exception when the incoming is total garbage. Other times I get a non-complete object structure when the incoming string is kind of OK - and finally it sometimes work. I've wrapped the conversion in a try/catch and than manually

JsonConverter CanConvert does not receive type

安稳与你 提交于 2019-12-10 12:45:04
问题 I have a custom JsonConverter , which doesn't seem to be called correctly. I have created the converter, added it to the JsonSerializerSettings.Converters collection and marked the property on the entity I am serialising with [JsonConverter(typeof(SearchGeoConverter))] , but even with these in place the converters CanConvert method never sees the type I am trying to convert. I only ever see, string , int and JObject . My converter looks like this: public class SearchGeoConverter :

Deserializing Noda Time's LocalDateTime with JSON.NET

这一生的挚爱 提交于 2019-12-10 12:44:25
问题 I'm trying to use Json.NET to serialize some Noda Time values and having trouble. Serialization is simple enough: LocalDateTime dt = ... // Assigned elsewhere LocalDateTimePattern isoDateTimePattern = LocalDateTimePattern.GeneralIsoPattern; JObject root = new JObject(); root.Add("time", isoDateTimePattern.Format(dt)); // Serialize other elements using (var sw = new StreamWriter(stream)) { serializer.Serialize(sw, root); } But deserialization is problematic. Json.NET seems to recognize the ISO

Error converting value to type 'System.Collections.Generic.List` in my json

一个人想着一个人 提交于 2019-12-10 12:25:57
问题 I am having problem in converting JSON to object in C#. I want to know that what's wrong with my json or object structure? Why its failing? My JSON is: [ { "added_on" : "10-03-2014", "country" : "INDIA", "crew" : "{\"Actor1\": \"Kangana\", \"Actor2\":\"Lisa Haydon\"}\r\n", "fb_link" : "https://www.facebook.com/Queenthefilm", "genre" : "DRAMA", "id" : 1, "image_first_url" : "", "image_main_url" : "133avk38CcBfNHsqwr0nzqOa8Db.jpg", "image_second_url" : "", "name" : "Queen", "ratings" : "[{\

Visual Studio cannot publish

不想你离开。 提交于 2019-12-10 11:47:49
问题 I tried this with a new project just to confirm that I have not made any errors, but Visual Studio 2019 won't publish any dot net core project. Below is the error I am getting... Any help would be much appreciated... Severity Code Description Project File Line Suppression State Error The "TransformAppSettings" task failed unexpectedly. System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one