json.net

How do I invoke WriteJson recursively?

青春壹個敷衍的年華 提交于 2019-12-06 04:57:50
I use Json.Net. When I serialize a Department2 object and WriteJson() is invoked I want it to be recursively invoked with each of the Telephone2 objects like I do in ReadJson() . How do I do that? using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; public interface ISimpleDatabag { string Databag { get; set; } } [JsonConverter(typeof(JsonDataBagCreationConverter<Department2>))] public class Department2 { public Telephone2[] Phones { get; set; } } [JsonConverter(typeof(JsonDataBagCreationConverter<Telephone2>))] public class Telephone2 { public string Name { get; set; } public

Use Json.NET to (de)serialize dictionary in the structure used by DataContractJsonSerializer? [duplicate]

試著忘記壹切 提交于 2019-12-06 04:55:08
This question already has answers here : Serialize dictionary as array (of key value pairs) (6 answers) Closed 3 years ago . Is there a way to use Json.NET for (de)serialization but continue to use the dictionary serialization conventions of DataContractJsonSerializer ? In other words, is there a way to read and write JSON in this structure: { "MyDict" : [ { "Key" : "One", "Value" : 1 }, { "Key" : "Two", "Value" : 2 } ] } Using a class like this (with Json.NET attributes): public class MyClass { public Dictionary<string, int> MyDict { get; set; } } It seems like Json.NET's

JSON.NET exception when deserializing an array of GUIDs

邮差的信 提交于 2019-12-06 04:53:08
I'm using JSON.NET to deserialize AJAX HTTP requests sent in from the browser, and am running into problems with web service calls that use a Guid[] as a parameter. This worked fine when I used the built in .NET serializer. First off, the raw bytes in the stream look like this: System.Text.Encoding.UTF8.GetString(rawBody); "{\"recipeIds\":[\"d9ede305-d244-483b-a435-abcf350efdb2\"]}" I then call: Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); .Type is System.Guid[] I then get

C# deserialize Json unknown keys

て烟熏妆下的殇ゞ 提交于 2019-12-06 04:26:55
问题 I have this JSON and i have to deserialize it: { "homepage": "http://files.minecraftforge.net/maven/net/minecraftforge/forge/", "promos": { "1.10-latest": "12.18.0.2000", "1.10.2-latest": "12.18.1.2014", "1.10.2-recommended": "12.18.1.2011", "1.5.2-latest": "7.8.1.738", "1.5.2-recommended": "7.8.1.737", "1.6.1-latest": "8.9.0.775", "1.6.2-latest": "9.10.1.871", "1.6.2-recommended": "9.10.1.871", "1.6.3-latest": "9.11.0.878", "1.6.4-latest": "9.11.1.1345", "1.6.4-recommended": "9.11.1.1345",

Converting arbitrary json response to list of “things”

为君一笑 提交于 2019-12-06 04:25:46
I'm having an unusual problem. It might not be a very realistic scenario, but this is what I have gotten myself into, so please bear with me. I have an API that returns Json and I'm using Json.NET to process the Json response. The problem is that the API can return a number of things and I have to be able to deserialize the response the following way: The API can return a single Json object. In this case I have to deserialize it into an ExpandoObject and put it into a List<dynamic> . The API can return null and undefined and the alike, in which case I have to return an empty list. The API can

IHttpActionResult with JSON string

倖福魔咒の 提交于 2019-12-06 04:22:25
问题 I have a method that originally returned an HttpResponseMessage and I'd like to convert this to return IHttpActionResult . My problem is the current code is using JSON.Net to serialize a complex generic tree structure, which it does well using a custom JsonConverter I wrote (the code is working fine). Here's what it returns: string json = NodeToJson(personNode); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(json, Encoding.UTF8,

Is it possible to specify an offset for JSON.NET deserialization of DateTimeOffset?

流过昼夜 提交于 2019-12-06 04:20:18
The following statement prints "1/1/0001 4:00:00 PM -05:00" Console.WriteLine(JsonConvert.DeserializeObject<DateTimeOffset>("\"0001-01-01T16:00:00\"")); This is because when json.net deserializes a DateTime string (which doesn't have an offset) to a DateTimeOffset object, it assigns the local offset, which in this case is -05:00. What if I don't want to use the local offset? Is there any way I can specify an offset to use for this deserialization? (The use case is the database server and the web server are in different time zones, and I need the incoming requests that have zone-unspecified

Web API/JsonMediaTypeFormatter accepts Invalid JSON and passes null argument to action

。_饼干妹妹 提交于 2019-12-06 04:06:02
问题 I have the following model: public class Resource { [DataMember(IsRequired = true)] [Required] public bool IsPublic { get; set; } [DataMember(IsRequired = true)] [Required] public ResourceKey ResourceKey { get; set; } } public class ResourceKey { [StringLength(50, MinimumLength = 1)] [Required] public string SystemId { get; set; } [StringLength(50, MinimumLength = 1)] [Required] public string SystemDataIdType { get; set; } [StringLength(50, MinimumLength = 1)] [Required] public string

How to parse nested JSON data structure

不想你离开。 提交于 2019-12-06 04:04:41
问题 In a Windows Phone app I need to parse JSON data. I am able to get the values for keys which aren't nested. But if the JSON has arrays inside arrays, then how do I extract values from the JSON file? In the past what I did was parse the JSON into a JArray object, then from the JToken I got the value of a specified string key. In the JSON below, people has men and women , and men itself has many men with different IDs. So if I have this complete thing as a JSON string, how do I print the value

C# : extract/retrieve child node from JSON structure

此生再无相见时 提交于 2019-12-06 04:03:27
问题 How can we extract or retrieve child nodes values from JSON structure in C#. my app is using OpenWeatherMap, I need to retrieve name from city , temp from list and description from weather nodes, my JSON and Class structure are below { "cod": "200", "message": 0.0284, "city": { "id": 2643743, "name": "London", "coord": { "lon": -0.12574, "lat": 51.50853 }, "country": "GB", "population": 0, "sys": { "population": 0 } }, "cnt": 1, "list": [ { "dt": 1429268400, "temp": { "day": 12.21, "min": 4