json.net

Deserializing an unknown type in JSON.NET

狂风中的少年 提交于 2019-12-18 05:50:17
问题 I just got a hold of JSON.NET and its been great so far. However, I cannot figure out how to determine the type of a serialized object when deserializing it. How can I determine the object's class to cast it? To clarify my question, let's say I wanted to do this string json = <<some json i don't know>> var data = JsonConvert.DeserializeObject(json); if (data is Person) { //do something } else if (data is Order) { //do something else } Does Json.NET support this kind of functionality? 回答1: you

Get Raw json string in Newtonsoft.Json Library

社会主义新天地 提交于 2019-12-18 05:46:11
问题 I have json like this { "name": "somenameofevent", "type": "event", "data": { "object": { "age": "18", "petName": "18" }, "desct": { } } } and I have 2 objects like this public class CustEvent { [JsonProperty("name")] public string Name { get; set; } [JsonProperty("type")] public string EventType{ get; set; } [JsonProperty("data")] public SomeData Data{ get; set; } } public class SomeData { [JsonProperty("object")] public String SomeObject { get; set;} [JsonProperty("dsct")] public String

Deserialize a List<AbstractClass> with newtonsoft.json

巧了我就是萌 提交于 2019-12-18 05:43:38
问题 i'm trying to serialize and deserialize a list of abstract classes ( mustinherit for vb), obviusly inside it there are only instances of derived classes. I've decorated the list parameter with the JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto) obtaining an output that look like this: But when i deserialize it keep saying that he cannot deserialize an abstract class. http://james.newtonking.com/json/help/index.html?topic=html/SerializeTypeNameHandling.htm public class ConcreteClass

Using JavaScript to deserialize references in a complex object graph from SignalR/Json.NET

自古美人都是妖i 提交于 2019-12-18 05:43:11
问题 I'm using SignalR to return a complex object graph to my JavaScript client. This object graph has multiple references throughout to the same object, so the JSON that SignalR/Json.NET returns looks a lot like this: { "$id": "57", "Name": "_default", "User": { "$id": "58", "UserTag": "ken", "Sessions": [{ "$id": "59", "SessionId": "0ca7474e-273c-4eb2-a0c1-1eba2f1a711c", "User": { "$ref": "58" }, "Room": { "$ref": "57" } }], }, "Sessions": [{ "$ref": "59" }] } (Of course, a lot more complicated

NewtonSoft JsonConverter - Access other properties

隐身守侯 提交于 2019-12-18 05:06:35
问题 I have a need to format the output json of a decimal to a currency, with the culture specified my the object I am serializing, the object could be nested so I cannot preset the option in the serializer. The current way I am doing this is by using extra string properties that format the output. [JsonIgnore] public decimal Cost {get;set;} [JsonIgnore] public CultureInfo Culture {get;set;} public string AsCurrency(decimal value) { return string.Format(this.Culture, "{0:c}", value); }

How do I skip default JavaScript array serialization for IEnumerable types in Json.Net?

[亡魂溺海] 提交于 2019-12-18 04:47:16
问题 Some custom types that implement IEnumerable don't necessarily have backing collections. They could be generated dynamically, for example using 'yield' or LINQ. Here is an example: public class SOJsonExample { public class MyCustomEnumerable : IEnumerable<KeyValuePair<int,float>> { public List<int> Keys { get; set; } public List<float> Values { get; set; } public MyCustomEnumerable() { Keys = new List<int> { 1, 2, 3 }; Values = new List<float> { 0.1f, 0.2f, 0.3f }; } public IEnumerator

Deserializing JSON object into a C# list

你。 提交于 2019-12-18 04:14:40
问题 I'm trying to deserialize a given JSON file in C# using a tutorial by Bill Reiss. For XML data in a non-list this method works pretty well, though I would like to deserialize a JSON file with the following structure: public class Data { public string Att1 { get; set; } public string Att2 { get; set; } public string Att3 { get; set; } public string Att4 { get; set; } } public class RootObject { public List<Data> Listname { get; set; } } My problem is with using JSON.Net's ability to create /

Web Api Model Binding and Polymorphic Inheritance

假如想象 提交于 2019-12-18 03:51:23
问题 I am asking if anyone knows if it is possible to to pass into a Web Api a concrete class that inherits from a abstract class. For example: public abstract class A{ A(); } public class B : A{ } [POST("api/Request/{a}")] public class Request(A a) { } At present I have looked around and most solutions seem to say that using TypeNameHandling will work. JsonMediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter(); jsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;

Newtonsoft JsonSerializer - Lower case properties and dictionary [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-18 03:48:52
问题 This question already has answers here : Keep casing when serializing dictionaries (4 answers) Closed 4 years ago . I'm using json.net (Newtonsoft's JsonSerializer). I need to customize serialization in order to meet following requirements: property names must start with lower case letter. Dictionary must be serialized into jsonp where keys will be used for property names. LowerCase rule does not apply for dictionary keys. for example: var product = new Product(); procuct.Name = "Product1";

Build JObject from JSONPath

守給你的承諾、 提交于 2019-12-17 22:28:55
问题 I'm a bit new to using the Newtonsoft JSON library for .NET. Is there any way to create a JObject or JToken from a JSONPath? So for example something like the following. string jsonPath = "$.ArrayA[0].ArrayB[0].Property"; JObject jObj = JObject.FromJSONPath(jsonPath); // SOMETHING LIKE THIS The result would be a JObject or JToken that looks like this. { "ArrayA": [{ "ArrayB": [{ "Property": "" }] } } 回答1: No. If you have some existing JSON, you can parse it to a JToken and then select one or