json.net

Converting newtonsoft code to System.Text.Json in .net core 3. what's equivalent of JObject.Parse and JsonProperty

断了今生、忘了曾经 提交于 2019-12-07 15:06:37
问题 I am converting my newtonsoft implementation to new JSON library in .net core 3.0. I have the following code public static bool IsValidJson(string json) { try { JObject.Parse(json); return true; } catch (Exception ex) { Logger.ErrorFormat("Invalid Json Received {0}", json); Logger.Fatal(ex.Message); return false; } } I am not able to find any equivalent for JObject.Parse(json); Also what will be the attribute JsonProperty equivalent public class ResponseJson { [JsonProperty(PropertyName =

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

假装没事ソ 提交于 2019-12-07 14:06:29
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? DDiVita This is my implementation I came up with: public class DictionaryConverter : JsonConverter {

How to detect duplicate keys in Web Api Post request Json

你说的曾经没有我的故事 提交于 2019-12-07 11:22:18
问题 I have a requirement to return a 400 error from an ASP.NET Web API Post request when the request Json contains duplicate keys. For instance if the request was { "key1": "value1", "key2": 1000, "key2": 2000, "key3": "value3" } then I would want the error to be thrown due to there being two "key2" keys. My controller method looks something like [HttpPost] public IHttpActionResult PostMethod([FromBody]RequestModel request) { ..... } and my RequestModel model like public class RequestModel {

Serializing to JSON, Nullable Date gets omitted using Json.NET and Web API despite specifying NullValueHandling

吃可爱长大的小学妹 提交于 2019-12-07 10:58:23
问题 using Newtonsoft.Json; namespace FAL.WebAPI2012.Controllers { public class Person { public int Id {get;set;} public string FirstName {get;set;} public string LastName {get;set;} [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include, NullValueHandling = NullValueHandling.Include)] public DateTime? Dob { get; set; } } public class TestNullsController : ApiController { // GET api/<controller> public Person Get() { Person myPerson = new Person() { Dob = null, FirstName = "Adrian", Id

MVC4: Include object name in Json

拟墨画扇 提交于 2019-12-07 09:25:05
问题 I have a question on a MVC4 (beta) issue I have been struggling with for some time now. The issue is that I want to create a json with the object name added for my webapi. The json has to be created this was as the receiving side needs this. My .NET/MVC knowledge is limited so please bear with me. I tried searching on the subject, but as MVC4 is still in beta it's difficult to find good info on this subject. I have already imported the JSON.NET formatter in my solution, but this does not add

Deserialize from json where can be single T object or array of T into List<T> [duplicate]

偶尔善良 提交于 2019-12-07 09:19:49
问题 This question already has answers here : How to handle both a single item and an array for the same property using JSON.net (7 answers) Closed 5 years ago . I have the code like this: var json = GetJsonData(path); JObject event_dates_data = JObject.Parse(json); var event_dates_list = JObject.Parse(event_dates_data["document"]["date"].ToString()); var event_dates = JsonConvert.DeserializeObject<List<EventDate>>(event_dates_list.ToString()); Json may contains an array objects (for example "date

Deserialize nested JSON to a flat class using Json.NET [duplicate]

梦想的初衷 提交于 2019-12-07 09:05:48
问题 This question already has answers here : Can I specify a path in an attribute to map a property in my class to a child property in my JSON? (4 answers) Closed 3 years ago . Given the following nested JSON string: string s = @" { ""id"": 10, ""fields"":{ ""issuetype"": { ""name"": ""Name of the jira item"" } } }"; How can I deserialize it to the following "flattened" class, using the JsonPropertyAttribute : public class JiraIssue { [JsonProperty("id")] public int Id { get; set; } [JsonProperty

JSON.NET Visual Studio 2008 and .NET 3.5 Compact Framework

邮差的信 提交于 2019-12-07 08:30:40
问题 Can I use JSON.NET in Visual Studio 2008 with .NET 3.5 Compact Framework? And how can I install/Configure it in the IDE? I've searched the internet but could not find it. I found this NuGet Support for Visual Studio 2008 to try install JSON.NET through NuGet but could not get it working. The result of this tutorial was an error Unable to find package 'your.package.name' : 回答1: I don't think NuGet will help you here. Json.NET removed support for the .NET 3.5 Compact Framework in release 4.0.1,

Deserializing json to C# list with existing items

帅比萌擦擦* 提交于 2019-12-07 08:08:23
问题 Given the following classes: class Report { public Report() { this.Fields=new List<Field>(); } [JsonProperty("fields")] public IList<Field> Fields { get; private set; } } class Field { [JsonProperty("identifier")] public Guid Identfier { get;set; } [JsonProperty("name")] public string Name { get;set; } } and the following test method set up: var report = new Report(); report.Fields.Add(new Field { Identifier = new Guid("26a94eab-3d50-4330-8203-e7750abaa060"), Name = "Field 1" }); report

Using Json.NET, deserialize to a property but has a numeric name

£可爱£侵袭症+ 提交于 2019-12-07 07:56:28
Got the Json { "commentary": null, "crimes": { "2010-12": { "anti-social-behaviour": { "crime_rate": "0.08", "crime_level": "below_average", "total_crimes": 47 } } } }}" Using Json.net to deserialize it, well restsharp which uses json.net. but it wont map to my classes as I cannot put a property called 2010-12 as the .net framework does not allow this. Any thoughts? Currently i got public class neighbourhoodcrimes { public String commentary { get; set; } public crimes crimes { get; set; } } public class crimes { public month _2010_12 { get; set; } } public class month { public category anti