天道酬勤,学无止境

json.net

Newtonsoft.Json Cannot create and populate list type

I have a model: [Serializable] public class User { public string UserID { get; set; } public string UserName { get; set; } public string Password { get; set;} public bool isLoggedIn { get; set; } public CookieCollection Cookies { get; set; } [NonSerialized] public string Response = ""; } Just trying to serialize and deserialize: string str = Newtonsoft.Json.JsonConvert.SerializeObject(u, typeof(User), settings); User us = Newtonsoft.Json.JsonConvert.DeserializeObject<User>(str); I am getting an error: Cannot create and populate list type System.Net.CookieCollection. Path 'Cookies', line 1

2022-01-18 12:11:29    分类:问答    c#   serialization   json.net   deserialization

Cannot Deserialize the Current JSON Object (Empty Array)

I am trying to make this program that formats all these objects into a treeview, to do this (I'm using JSON for ordering the objects), I needed to parse the JSON, so I chose JSON.NET. So here is an example of how the formatting is: { "Space": { "ClassName": "SpaceObject", "Name": "Space", "Children": { "Object1": { "ClassName": "Object", "Name": "Object1", "Children": [] }, "Object2": { "ClassName": "Object", "Name": "Object2", "Children": [] } } } } public class CObject { [JsonProperty(PropertyName = "Name")] public string Name { get; set; } [JsonProperty(PropertyName = "ClassName")] public

2022-01-18 11:46:09    分类:问答    c#   json   json.net

Access the default JSON.NET serializer in ASP.NET Web API

I have a ASP.NET web API project and since it does not support having 2 body parameters, I use a JObject parameter and then extract the actual parameters from it. Like this. Public bool mymethod(JObject data){ myclassA a = data["a"].toObject<myclassA>(); myclassA b = data["b"].toObject<myclassB>(); } But the 2 class types implement ISerializable and I need the JSON.NET to ignore it. I have set up the default JSON.NET serializer to do that and it works fine when serialization is done automatically. But I need to get a reference to the built in JSON.NET serializer so that I could use it like

2022-01-18 06:08:16    分类:问答    asp.net-web-api   json.net

parsing nested json with json.net

I have problems with json deserialization , below is my json { "_id" : ObjectId("56bc28c436b252c406a67f17"), "empname": "dhiraj", "empcode": "123a", "level": { "levelID": 3, "levelDescription": "manager", "levelCode": "mg" }, "Address": [ { "Home": { "streetname": "Home", "city": "bbb", "state": "aaa" } }, { "Office": { "streetname": "ofc", "city": "ccc", "state": "ddd" } } ] } And for above json the object classes are like public class Employee { public ObjectId _id { get; private set; } public string empname { get; set; } public string empcode { get; set; } public List<Level> level { get

2022-01-18 04:52:04    分类:问答    c#   json   c#-4.0   json.net

Deserialize Dynamic JSON file C# NewtonSoft.JSON

Working on deserializing a dynamic JSON file which could contain 2 separate classes and I won't know which type of data will be in the array. The problem is, I deserialize the root object to type "Base", "subtests" objects are deserialized to "Subtest", but the "subtests" array could be of type "Base" or of type "Subtest". QUESTION: How would I go about programatically determining that if the object contains "subtest", I deserialize to Base, and if it doesn't, it should deserialize to "Subtest"? I really appreciate the help on this one since I'm on a short timeline. (EDIT: Added comments to

2022-01-17 16:26:14    分类:问答    c#   json   json.net   deserialization

C# format JSON with backslash '\' in value

I have some JSON from a third party system that contains backslashes in the value. For example: string extract = @"{""key"": ""\/Date(2015-02-02)\/""}"; which without the c# string escaping corresponds to the string: {"key": "\/Date(2015-02-02)\/"} I'd like to be able to format (e.g. indent) this JSON. Typically for formatting, I might use something like JsonConvert like so: JsonConvert.SerializeObject(JsonConvert.DeserializeObject(extract), Formatting.Indented) This doesn't quite work, as it sees the value as a date, but as it's not in the standard MS format of \/Date(ticks)\/, it goes to a

2022-01-17 05:12:55    分类:问答    c#   json   serialization   json.net

Populating non-serializable object with Json.NET

In a test I want to populate an object (a view model) from a JSON string. For example, the target object has this property: public string Query { get; set; } So I want to be able to do this: var target = ...; JsonConvert.PopulateObject(target, "{ 'Query': 'test' }"); However, the Query property is not being set. Debugging through the code, it appears that properties on target are ignored because member serialization is opt-in. Since the target class is not a data contract and is not populated in this way outside of unit tests, I cannot opt it into member serialization via attributes. I can't

2022-01-17 04:56:12    分类:问答    c#   .net   json   json.net

Json.net no longer throws in case of duplicate

I'm trying to upgrade my C# application from Newtonsoft.JSON 6 to the latest version (9.0.1). I noticed a change of behavior when deserializing object containing duplicate elements, such as : { "name": "test", "data": { "myElem": 1, "myElem": 2 } } When deserializing such object, Json.net was previously throwing an ArgumentException. Now the deserialization succeed and it seems that it uses the value of the last duplicated key (hence "2" in above example). From what I read, there is some debate around duplicates but the RFC only recommend to have unique keys. I'd like to keep the previous

2022-01-17 03:01:36    分类:问答    c#   json   duplicates   json.net   deserialization

Using custom JsonConverter in order to alter the serialization of the portion of an object

I'm having a hard time overriding the WriteJson method of a custom JsonConverter in order to slightly alter the way serialization is performed. I need to call a REST service that accepts a certain input that has a generic portion. I can reproduce the problem I'm having with the following payload format : public sealed class JsonField { public string Key { get; set; } public object Value { get; set; } public string OtherParam { get; set; } } public sealed class JsonPayload { public string Item1 { get; set; } public string Item2 { get; set; } public List<JsonField> Fields { get; set; } } The

2022-01-17 03:00:45    分类:问答    c#   json.net

Retrieve required string from bigger JSON string

I have JSON string returned by method: public List<Issues> Getdata( string jql, List<string> fields = null, int startAt = 0, int maxResult = 500) { string data = JsonConvert.SerializeObject(request); string result = runQuery(JiraResource.search, data, "POST"); //returns results and working fine SearchResponse response = JsonConvert.DeserializeObject<SearchResponse>(result); return response.IssueDescriptions = GetIssues(response);; } and JSON string look like this: {"expand":"schema,names","startAt":0,"maxResults":50,"total":139,"issues":[{"expand":"operations,versionedRepresentations,editmeta

2022-01-17 02:51:24    分类:问答    c#   .net   json   json.net