json.net

ASP.NET Core MVC - empty string to null when sending JSON to server

∥☆過路亽.° 提交于 2019-12-11 08:13:18
问题 When posting input data as a FormData to the ASP.NET Core MVC controller, by default empty string values are coerced to null values. However, when sending input data as JSON to the controller, empty string values remain as they are. This leads to different behavior when validating string properties. For example, description field is not bound to null , but to empty string on the server: { value: 1, description: "" } This in turn makes following model invalid, even though Description is not

string behavior in json.net: why is a null JToken replaced with a non-null value?

拜拜、爱过 提交于 2019-12-11 08:13:08
问题 This using json.net (C#). Not sure why JToken is not null. var obj = new JObject(); obj["field1"] = null; var token = obj["field1"]; Console.WriteLine(token == null); // false Console.WriteLine(token.ToString() == ""); // true Console.WriteLine(token.Type == JTokenType.Null); // true 回答1: This is by design. Json.NET never allows an actual null JToken value to appear in a JToken hierarchy, either as an JArray member or a JProperty value. Instead, if applications code attempts to set or add a

JSON.NET, XmlSerializer and “Specified” property

元气小坏坏 提交于 2019-12-11 07:55:59
问题 I have a REST service which takes JSON and XML as input and does a SOAP call to an extenal service with the deserialized content. The classes which are used for deserialization are auto-generated from the wsdl of the SOAP service. I use the XmlSerializer in case of a XML request and I want to use the Newton JSON.NET JsonSerializer for JSON. Now I have the problem, that the WSDL generated classes contain the "Specified" property for optional atomar values (such as bool, int etc.). This is

How to format List<List<int>> as a table in JSON file?

人走茶凉 提交于 2019-12-11 07:54:19
问题 I use Newtonsoft.Json library for serializing and deserializing .NET objects. One of the things I am serializing are tables (represented as List<List<int>> ). What I need is to format them in the serialized file like a table, not like a bunch of columns as they are formatted now. Is there any way to achieve this? Now, I have the following formatting in the json-file: "Table": [ [ 1024, 1024, 1024, 1024 ], [ 1024, 1024, 1024, 1024 ], [ 1024, 1024, 1024, 1024 ] ] What I want to achieve is this:

How do I parse a JSON string to a C# object using inheritance / polymorphism

∥☆過路亽.° 提交于 2019-12-11 07:50:06
问题 I want to parse a JSON string to a C# object that could be polymorph. To sum it up: I don't want to instantiate the root object, but i want to instantiate the inherited object depending on the JSON input. Here's an example of the C# objects I use: public class Module { public string name; } public class Wheel : Module { public int amount; public Wheel(string name, int amount) : base(name) {...} } public class Break : Module { public double delay; public Break(string name, double delay) : base

Rest exception: Unable to find a constructor to use for type “myclass”. A class should either have a default constructor

血红的双手。 提交于 2019-12-11 07:37:32
问题 I know this question has been asked before, but I can't find an answer to solve this problem. I'm making a request to a web service which returns a json, and then I save that json as an object in a list using json.net. List<myclass> result; var request = new RestRequest(url, Method.POST); //Set the parameters of the request //[...] IRestResponse response = client.Execute(request) Console.WriteLine(response.Content); //response.Content = [{"nomPrecio":"string","nomPrecioEN":"string",

How to deserialize a json in .net core 3.0

房东的猫 提交于 2019-12-11 07:28:06
问题 I have been facing some trouble to deserialize a json using .Net Core 3.0 Blazor app. The Framework i am using .Net Core 3.0 Blazor with Visual Studio Preview 2019 { "cols": [ "ID", "LastName", "Firstname", "middlename", "Suffix", "Title" ], "rows": [ [ "90", "Dada", "Mama", "", "", "" ] ] } Expected result: i want to load this json dynamically as a table in the web form using blazor. I don't mind to use any UI framwork for this like devexpress or telerik. id LastNmae Firstname middlename

Json.NET: Serialization Callbacks in base object

六月ゝ 毕业季﹏ 提交于 2019-12-11 07:18:46
问题 Using Json.NET are Serialization Callbacks supported when the OnDeserializedAttribute is placed on a base class method? For example using this object graph: [DataContract] public class StubData:StubBase {} [DataContract] public class StubBase { public string Id { get; set; } [OnDeserialized] public void OnDeserialized(StreamingContext context) { Id = "1"; } } var stubData = JsonConvert.DeserializeObject<StubData>(@"{""anyData"":""Foo""}"); stubData.Id //returns 1 回答1: Yes. OnDeserialized is

Cannot deserialize the current JSON array when returned value can be either array or single item

こ雲淡風輕ζ 提交于 2019-12-11 07:18:39
问题 I am new to Newtonsoft.Json so please excuse my ignorance - however I am coming up against this issue when trying to Deserialize the following Json to either a c# object or indeed manually. The Json is { "travellerInfo": [ { "passengerData": { "travellerInformation": { "passenger": [ { "type": "ADT", "firstName": "MARY MRS" }, { "type": "INF", "firstName": "JOSHUA" } ], "traveller": { "surname": "SMITH", "quantity": "2" } } } }, { "passengerData": { "travellerInformation": { "passenger": {

C# Parsing json that may have simple types

大城市里の小女人 提交于 2019-12-11 07:07:07
问题 From C# I'm calling RESTful web service that returns JSON. The service can return an arbitrary Javascript object, we don't know its structure at compile time. Usually, it returns a JSON object, but there are situations where it returns a simple string. JSON.Net throws a runtime error when I try to deserialize the simple string. dynamic dyn = JsonConvert.DeserializeObject("just a string"); throws: Newtonsoft.Json.JsonReaderException was unhandled by user code HResult=-2146233088 Message