json.net

deserialize type handled json using Newtonsoft library between different applications

限于喜欢 提交于 2019-12-25 16:59:07
问题 I have a data model in one application that I wish to exchange over TCPIP with another application. I need to use type handling because I want to have a single list of an abstract type containing derived types, as opposed to multiple lists for each derived type. As illustrated here: This : public Dictionary<int, Parameter> Parameters { get; set; } Instead of this: public Dictionary<int, BooleanParameter> BooleanParameters { get; set; } public Dictionary<int, AnalogParameter> AnalogParameters

Applying a Contract Resolver to a specific model via an attribute

ぃ、小莉子 提交于 2019-12-25 09:45:21
问题 I am using Web API which internally uses Json.Net. Currently, I have a scenario where I need to apply a custom contract resolver only to one specific model. I can apply a custom JsonConverter using an attribute like [JsonConverter(typeof(MyConverter))] . Is it possible to apply a custom contract resolver on one model in the same way? In my scenario, I need to be able to use [JsonProperty] attributes during deserialization but ignore them during serialization, just for one of my model classes.

Can't parse JSON data to .NET DateTime

瘦欲@ 提交于 2019-12-25 09:24:37
问题 It's pretty simple. I have a string string s = "/Date(1474408920000)/" And I want to convert it to a date: DateTime date = JsonConvert.DeserializeObject<DateTime>(s); But I get the error: "Error parsing comment. Expected: *, got D. Path '', line 1, position 1." What's going on here? Thanks for your help! 回答1: Your json string is not valid but can easily be fixed by surrounding it with " string s = @"""/Date(1474408920000)/"""; Now DateTime date = JsonConvert.DeserializeObject<DateTime>(s);

Can't deserialize json array

风流意气都作罢 提交于 2019-12-25 08:57:53
问题 I know this question has been asked many times. But i'm still a newbie and it seems like I can't deserialize the json array I got from a get request. This is my code: Activity.cs var getResponse = RequestClass.GetChargingPointsData(pathUrl, currentToken); System.Diagnostics.Debug.WriteLine("My GETresponse: " + getResponse); //CharchingPointClass getCPDetail = JsonConvert.DeserializeObject<CharchingPointClass> (getResponse); //var getCPDetail = JsonConvert.DeserializeObject<CharchingPointClass

Unquoted json property name

梦想与她 提交于 2019-12-25 08:57:31
问题 One of the features in NewtonSoft's Json.Net parser is the support of unquoted property name, for example {test:"abc"}. Is it possible to turn off this feature so the json parser will throw an error when it parses a json string with unquoted property name? 回答1: What you can do is to deserialize and then serialize again and compare with original input. That will detect missing quotes. Even if this is not performance efficient, it is very simple and serves well when the relevant code

NuGet Json.Net Install Erros VS 2010 Shell

六眼飞鱼酱① 提交于 2019-12-25 08:36:51
问题 I am trying to install NuGet do that I can use Json.Net Extensions. I have installed version 2.8.60318.667 (which was the download from Microsoft for VS2010. When I try and install Json.Net by the Package Manager Console it fails PM> Install-Package Newtonsoft.Json Attempting to resolve dependency 'Microsoft.CSharp (≥ 4.3.0)'. Install-Package : 'Microsoft.CSharp' already has a dependency defined for 'System.Dynamic.Runtime'. At line:1 char:16 + Install-Package <<<< Newtonsoft.Json +

extract data from json in asp.net c#

霸气de小男生 提交于 2019-12-25 07:37:07
问题 I'm trying to extract some data from json. I've been looking for a solution either in JSS or Json.net but haven't been able to figure this problem out. this is how my Json looks like: Note: i Have tested and the mapping and decentralization works! I'm looking for a way to extract specifc data from the json! Thanks in Advance! { "tasks":[ { "id":"tmp_fk1345624806538", "name":"Gantt editor ", "code":"", "level":0, "status":"STATUS_ACTIVE", "start":1346623200000, "duration":5, "end"

JsonResult not returning (valid) json - Serializing going wrong?

 ̄綄美尐妖づ 提交于 2019-12-25 07:26:28
问题 I try to make a simple call to a server-side method in ASP.NET MVC 4. I do this by making a jQuery call to a method. The method gets called and the properties are correct, but the response of this method is not correct. I try to serialize an .NET object using JSON.NET. I can see I get the following response (using FireBug): "{\"VisitorId\":\"11a0606b-5336-4fa7-b50f-3edf97d8301b\",\"PhoneToTransfer\":\"61793650\",\"PhoneToNotify\":\"\",\"EmailToNotify\":\"mcoroklo@gmail.com\",\"EmailToTransfer

Deserialize Json.net JSON File directly to a C# dictionary type?

喜欢而已 提交于 2019-12-25 07:24:50
问题 Is there a way using JSON.NET to deserialize a file type directly to a C# dictionary type? For example: using (StreamReader file = File.OpenText(myFileName)) { Dictionary<string, int> mydictionary = new Dictionary<string, string>(); JsonSerializer serializer = new JsonSerializer(); //is there a json.net format to make the next line work mydictionary = (JSONParameters)serializer.Deserialize(file, typeof(JSONParameters)); //return mydictionary; } I said "FILE" not dictionary to dictionary..

Serializing a list of interface objects

邮差的信 提交于 2019-12-25 07:16:46
问题 Using Json.net, I want to deserialize a basket containing interface objects. This... { "Owner": "John", "Fruit": [ <an apple object>, <a pear>, etc... ] } ... should go into this... class Basket { string Owner; List<iFruit> Fruit; //contains instances of Apple, Pear,... } Interfaces cannot be instantiated, so a conversion to concrete objects is needed. I found examples using a JsonConverter to create concrete Apple and Pear instances. But the list is always created directly with a line like: