json.net

Deserializing JToken content to an Object

会有一股神秘感。 提交于 2019-12-04 15:13:02
问题 I want to deserialize JToken content to an object ( User ). How am I able to do this? Here is my json string: string json = @"[{""UserId"":0,""Username"":""jj.stranger"",""FirstName"":""JJ"",""LastName"":""stranger""}]"; This being sent to an api parameter as JToken . User class: public class user { public int UserId {get; set;} public string Username {get; set;} public string FirstName {get; set;} public string LastName {get; set;} } Web Api Method: public IHttpActionResult Post([FromBody

C# 使用Json.NET对数据进行序列化和反序列化

徘徊边缘 提交于 2019-12-04 15:12:44
本文首发于个人博客 https://kezunlin.me/post/22391aa3/ ,欢迎阅读最新内容! c# json serialize and deserialize using json.net JsonConvert <!--more--> Guide Json.NET JsonConvert.SerializeObject JsonConvert.DeserializeObject install Use NuGet to download the package "Project" -> "Manage NuGet packages" -> "Search for "newtonsoft json". -> click "install". code reference using Newtonsoft.Json; serialize collections Product p1 = new Product { Name = "Product 1", Price = 99.95m, ExpiryDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc), }; Product p2 = new Product { Name = "Product 2", Price = 12.50m,

JToken is not a reference of JObject?

核能气质少年 提交于 2019-12-04 15:01:38
I have not yet noticed that James Newton King wrote or spoke about what JToken is . I have made the incorrect assumption that it somehow holds a reference to JObject . This is not the case as the these LINQPad statements demonstrate: var json = @" { ""item"": { ""foo"": ""4"", ""bar"": ""42"" } } "; var jO = JObject.Parse(json); var jToken = jO["item"]["foo"]; jToken = "5"; jO.ToString().Dump("jO"); jToken.Dump("jToken"); The output: jO { "item": { "foo": "4", "bar": "42" } } jToken 5 Should not jO["item"]["foo"] == 5 ? First, let's talk about what a JToken is. JToken is the abstract base

Convert DataSet with multiple Datatables to Json

自作多情 提交于 2019-12-04 14:23:35
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 other .net Json tools? You can get the JSON you want by implementing a custom JsonConverter for the

How to use JSON.Net to read JSON and output HTML?

偶尔善良 提交于 2019-12-04 14:19:09
How can I use JSON.Net and loop through the following JSON to output one HTML image tag (a string) for each member of the "photos" object? My goal is to read the below JSON and output this string: "<img src='/images/foo.jpg' alt='Hello World!'><img src='/images/bar.jpg' alt='Another Photo' />" JSON is stored in external file "photos.json" { "photos": { "photo1": { "src": "/images/foo.jpg", "alt": "Hello World!" }, "photo2": { "src": "/images/bar.jpg", "alt": "Another Photo" } } } I've started with code similar to what's shown here: http://www.hanselman.com/blog

Newtonsoft JSON PreserveReferencesHandling with custom Equals usage

♀尐吖头ヾ 提交于 2019-12-04 13:48:21
I'm currently having some issues with Newtonsoft Json. What I want is simple: Compare the Object which will be serialized with all Properties and Subproperties for Equality. I tried now to Create my own EqualityComparer but it only compared with the Properties of the Parent Object. Also, I tried to write my own ReferenceResolver but had no luck with it. Let's talk with an Example: public class EntityA { int Foo {get; set;} public override bool Equals(object obj) { return (obj is EntityA other) && other.Foo == this.Foo; } } public class EntityB { int Bar {get; set;} EntityA Parent {get; set;}

Remove Null values in JSON and Update JSON

浪尽此生 提交于 2019-12-04 13:47:27
I have JSON Array as a string by serializing a list using Newtonsoft as below [{"ID":"1","Name":"somename","Class":"12","Section":null},{"ID":null,"Name":"somename","Class":"13","Section":null},{"ID":2,"Name":"somename","Class":null,"Section":"A"}] I need to convert this JSON by removing the NULL values to another JSONString like below [{"ID":"1","Name":"somename","Class":"12",},{"Name":"somename","Class":"13",},{"ID":2,"Name":"somename","Section":"A"}] Is there a way I can use Newtonsoft for this or how do i do this. You can use JsonSerializerSettings with NullValueHandling : var result =

JsonConverter equivalent for HTTP GET parameter

做~自己de王妃 提交于 2019-12-04 13:21:36
When writing C# web API controllers for HTTP POST functions, I can use attributes from Newtonsoft JSON on the properties of the parameter object. In particular, I can use a JsonConverter attribute on properties of an enum type to convert a string representation received from the client into one of the enum values (and back, for response objects): public class MyArgumentsDTO { [JsonConverter(typeof(SomeEnumConverter))] public SomeEnum MyValue { get; set; } } // in the controller: [Route("doSomething")] [HttpPost] public Boolean DoSomething(MyArgumentsDTO options); However, what should I do for

Json.NET - controlling class object-properties deserialization

这一生的挚爱 提交于 2019-12-04 13:15:41
I have a model class Link which is deserialized with JSON.Net. public class Link { [JsonConstructor] internal Link(int id) { Id = id; } public int Id { get; internal set; } [JsonProperty("title")] public string Title { get; internal set; } [JsonProperty("description")] public string Description { get; internal set; } ... and so on public Avatar AuthorAvatar { get; internal set; } } Avatar contains three properties: DefaultImageUri , SmallImageUri , MediumImageUri . Is it possible to create Avatar object on Link object deserialization which would use: author_avatar , author_avatar_small ,

JSON.NET Deserialization Property Name conversion to ExpandoObject with custom ContractResolver

二次信任 提交于 2019-12-04 13:02:40
I have this JSON: {"firstName": "John","lastName": "Doe"} This JSON.NET Contract Resolver: public class CustomContractResolver : DefaultContractResolver{ protected override string ResolvePropertyName(string propertyName) { return propertyName.Replace("_",""); } } And I have this WebApi Controller method with uses an expando to a partial update of a db row using the provided fields: public virtual int Post(int id, JObject content) { var obj = JsonConvert.DeserializeObject<ExpandoObject>(content.ToString(), new JsonSerializerSettings { ContractResolver = new CustomContractResolver() }); db