json.net

JSON.NET deserialize JSON object stored as property

雨燕双飞 提交于 2019-12-20 04:15:11
问题 I have a JSON message to deserialize with a string property containing the JSON of another object. I have the following classes public class Envelope { public string Type { get; set; } public Message InnerMessage { get; set; } } public class Message { public string From { get; set; } public string To { get; set; } public string Body { get; set; } } the JSON message I receive is in this format: { Type : "send", InnerMessage : "{ From: \"sender\", To: \"receiver\", Body: \"test\" }" } note that

Issue with serializing data using JSON.Net

冷暖自知 提交于 2019-12-20 04:08:02
问题 I am using a Kendo Scheduler in my app to pull data from my database using a Web Api. I created a Web Api function and just hard coded some data in there to be sure the Kendo Scheduler could read my data. Here is my code for the Api function: [Route("api/v1/Events/GetPersonalEvents", Name = "ApiEventsGetPersonalEvents")] [HttpGet] public DataSourceResult GetPersonalEvents([System.Web.Http.ModelBinding.ModelBinder(typeof(WebApiDataSourceRequestModelBinder))]DataSourceRequest request) { var q =

Removing empty array members from a JSON string

一笑奈何 提交于 2019-12-20 04:07:05
问题 I have a JSON string as shown below. I want to remove the empty array objects from it programmatically so that I can convert it to a DataTable . Here is an example of my JSON: { "result":[ { "id":"1", "name": "Temp", "property":[] }, { "id":"2", "name": "Temp2", "property":[] } ] } You can see that the property member in each result is an empty array. Any suggestion on how I can remove it? Currently I'm doing the following to convert the JSON to a DataTable : DataTable dt = JsonConvert

Add a field into JSON array

梦想的初衷 提交于 2019-12-20 04:04:46
问题 I got a JSON string with an array like this: { "Id": 123, "Username": "Sr. X", "Packages": [ { "Name": "Cups", "SupplierId": 1, "ProviderGroupId": 575, "SupplierName": "Foo Cups" }, { "Name": "Pins", "SupplierId": 5, "ProviderGroupId": 1082, "SupplierName": "Foo Pins" } ] } and I want to add a new field into Packages array like: "Packages": [ { "Name": "Cups", "SupplierId": 1, "ProviderGroupId": 575, "SupplierName": "Foo Cups", "New Field": "Value" },... Right now I can add a new field but in

Deserialize JSON dictionary values with JsonConverter

空扰寡人 提交于 2019-12-20 03:29:26
问题 I use Json.NET library to deserialize JSON. For an abstract class Foo I have a custom JsonConverter . This is the way I have used it: [JsonConverter(typeof(FooJsonConverter))] public Foo MyFoo { get; set; } So far so good. The problem arises when I use the Foo class in a Dictionary. This was my attempt: [JsonDictionary(ItemConverterType = typeof(FooJsonConverter))] public Dictionary<string, Foo> MyFooDictionary { get; set; } But the above gives the error: Attribute 'JsonDictionary' is not

empty DataTable to Json with columns name

蓝咒 提交于 2019-12-20 03:29:11
问题 My app is divided in two parts : client and server. The server side ask a sql database and store the results in a DataTable. Then i use the next function to convert the DataTable to Json to send it to client. The problem is when there are no results, i get an empty string. I would like to get a string which will allow the client side to display an empty datatable but with column names, i mean with column header ! Here is the function on the server side to convert datatable to Json: public

C# Newtonsoft JSON library outputs different wrong JSON keys for same dataset on different computers

折月煮酒 提交于 2019-12-20 03:19:44
问题 I have no clue whatsoever on how to resolve this. All I can figure out as a way of starting to resolve this is that my dev machine where things work perfectly is a 32 bit and the other tested computers are 64 bit (incidentally there are no other 32 bit machines around me to test with). Regardless, this is a strange scenario. Now to the issue: I am using Newtonsoft JSON library in my c# application. I have the following c# class to serialize: class sync_object { //newtonJson give bad keys to

How do you modify the Json serialization of just one field using Json.net?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 02:53:10
问题 Say for example I'm trying to convert an object with 10 fields to Json, however I need to modify the process of serializing 1 of these fields. At the moment, I'd have to use manually write out each property like this: public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteStartObject(); writer.WritePropertyName("Field1"); serializer.Serialize(writer, value.Field1); writer.WritePropertyName("Field2"); serializer.Serialize(writer, value.Field2);

Json.NET Uri (de)serialization errors

空扰寡人 提交于 2019-12-20 02:47:06
问题 I need to serialize and deserialize an object that contains an System.Uri property using the latest (4.0.3) Json.NET library. The following code demonstrates the problem: string input = "http://test.com/%22foo+bar%22"; Uri uri = new Uri(input); string json = JsonConvert.SerializeObject(uri); Uri output = JsonConvert.DeserializeObject<Uri>(json); The DeserializeObject method throws an JsonReaderException. This works fine with 4.0.2. I've submitted an issue on codeplex with tests and patch to

How to parse Json.NET polymorphic objects?

江枫思渺然 提交于 2019-12-20 02:36:08
问题 I've written a web service that sends and returns json created with Json.NET. I've included typenames, which allows polymorphism. With a bit of hacking, I've got this working with a silverlight client, but I don't know how to make it work for javascript clients. How can I parse this using javascript? { "$type": "MyAssembly.Zoo, MyAssembly", "ID": 1, "Animals": [ { "$type": "MyAssembly.Dog, MyAssembly", "LikesBones": true, "Name": "Fido" }, { "$type": "MyAssembly.Cat, MyAssembly", "LikesMice":