json.net

Deserialize JSON as object or array with JSON.Net [duplicate]

会有一股神秘感。 提交于 2019-12-10 03:00:55
问题 This question already has answers here : How to handle both a single item and an array for the same property using JSON.net (7 answers) Closed 27 days ago . I want to know if it is possible to deserialize a JSON object that could either be an object or an array. Similar to this question: Jackson deserialize object or array But using JSON.Net. Example { "response": { "status":"success", // Could be either a single object or an array of objects. "data": { "prop":"value" } // OR "data": [ {"prop

type or namespace name 'Newtonsoft' could not be found

十年热恋 提交于 2019-12-10 02:54:16
问题 I've looked this question up, but I don't see too many answers, and obviously none have been helpful or I wouldn't be asking. I am a .NET neophyte. My local environment is Win7, Microsoft Virtual Web Developer 2010 Express. I added the NewtonSoft.Json as a custom component library. I built a simple "HelloWorld" web service, using Newtonsoft.Json custom components. When I do a build on my local machine (Visual Web Developer 2010 Express) it works great. I actually get a valid JSONP output, not

Serialize data to json string with dynamic property names

余生长醉 提交于 2019-12-10 02:54:01
问题 I have a method which accepts a key and a value. Both variables can have a dynamic content. key => is a dynamic string which can be everything like e.g. "LastSentDate" value => is an object which can be everything like e.g. "2014-10-10" As key is a dynamic value like "LastSentDate" or whatever key is passed to the method then I want that the json property is the value of the key string and not literally key itself... public void SetRowVariable(string key, object value) { var obj = new { key =

json.net required property not found in json

梦想与她 提交于 2019-12-10 02:50:19
问题 I am using Json.net, I got a class as following public class RecordAlias { [JsonProperty(PropertyName = "eId", Required = Required.Always)] public string EntityId { get; set; } [JsonProperty(PropertyName = "aId", Required = Required.AllowNull)] public string AliasId { get; set; } [JsonProperty(PropertyName = "iSd", Required = Required.AllowNull)] public bool IsSelected { get; set; } } So that following json can be deserialized even through some items don't have property "iSd" in json string,

Provide ArrayPool object to JsonOutputFormatter constructor

你离开我真会死。 提交于 2019-12-10 02:44:43
问题 After upgrading from .net RC2 to RTM I find I need to supply a parameter to a constructor of JsonOutputFormatter that derives from ArrayPool. How do I get this object? I am newing JsonOutputFormatter manually because I need to configure ReferenceLoopHandling. Only other related info I could find is this: https://github.com/aspnet/Mvc/issues/4562 public IServiceProvider ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMemoryCache(); services.AddSession();

Confusion in getting Parent from JToken

杀马特。学长 韩版系。学妹 提交于 2019-12-10 02:37:59
问题 I have the following JSON document stored in a text file { "attributes": {"attr0":"value0"}, "children" : { "ProductA" : { "attributes": {"attr1":"value1", "attr2":"value2"}, "children" : { "ProductC":{ "attributes": {"attr3":"value3", "attr4":"value4"}, "children" : {}, "referencedChildren" : {} } }, "referencedChildren" : {} }, "ProductB" : { "attributes": {"attr5":"value5", "attr6":"value6"}, "children" : {}, "referencedChildren" : {} } }, "referencedChildren" : {} } I have written this

Using JSON.Net to write a property name

妖精的绣舞 提交于 2019-12-10 02:29:18
问题 I am using JSON.net to write some json in C#. I can produce JSON like this { "id": "234", "name": "abc" } What i would like to do is get is this { "DATA": { "id": "234", "name": "abc" } } Here is the json.net code i'm using StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); JsonWriter jsonWriter = new JsonTextWriter(sw); jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("id"); jsonWriter.WriteValue("234");

JSON.NET Selecting items in array with linq

a 夏天 提交于 2019-12-10 02:06:36
问题 I need to select some values from a json response. Im using json.net, fine with the simpler stuff, but there doesnt seem to be much documentation/tutorials on anything past that. In the json example below i need to select all the ages: { "teacherHolder": [{ "id": 200000001, "name": "Mr Test", "class": "a4", "students": [{ "id": "100532469", "name": "ben" }, { "id": "100506025", "name": "bill" }, { "id": "100000447", "name": "bob" }] }] } I have tried this and other variations: var stuff =

Deserializing such that a field is an empty list rather than null

好久不见. 提交于 2019-12-10 01:52:10
问题 If I have a class like this: [DataContract(Name = "", Namespace = "")] public class MyDataObject { [DataMember(Name = "NeverNull")] public IList<int> MyInts { get; set; } } Is there a way I can make MyInts field a non-null empty list when the following string is deserialized? string serialized = @"{""NeverNull"":null}"; MyDataObject myDataObject = JsonConvert.DeserializeObject<MyDataObject>(serialized); I’m using Newtonsoft.Json The reason I ask is that I have a fairly complicated json

Why System.Version in JSON string does not deserialize correctly?

心已入冬 提交于 2019-12-10 01:14:22
问题 Context: I need to pass an object containing a large number of properties/fields (to UI Layer from Middle Tier Layer). Among this list of properties, one is of type Version which is not getting deserialized correctly from JSON string format. I have a chosen JSON format over XML as JSON serialization to string will return short string result. Problem: System.Version does not get deserialized correctly. I have tried two different .NET Libraries. Following are the code snippets for each: Code