json.net

Customize identation parameter in JsonConvert.SerializeObject

梦想与她 提交于 2019-12-12 16:53:43
问题 The default ident in Json.Net seems to be 2 spaces: var result = JsonConvert.SerializeObject(jsonString, Formatting.Indented); For clarity I want to change it to 4 spaces, but I don't seem to find the right way to apply the property. It seems that it exists, since I have found some similar code (direct link here): using (JsonTextWriter jw = new JsonTextWriter(sw)) { jw.Formatting = Formatting.Indented; jw.IndentChar = ' '; jw.Indentation = 4; jw.WriteRaw(config.ToString()); } ...except that,

JSON.Net Duplicate properties on Serialization of derived object where base contains Dictionary Properties

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 16:52:27
问题 I currently have a problem whereby when I serialize a class that derives from a base class that contains a Dictionary property, duplicate properties are serialized to JSON. The derived class contains a concrete property definition AND the dictionary in the base class contains a property of the same name at runtime. Upon Serialization the generated JSON string will contain two of the same property which is invalid JSON and will trip up Deserializers. I need a generic way for the derived class

Dealing with fanart.tv webservice response JSON and C#

懵懂的女人 提交于 2019-12-12 16:35:23
问题 I am trying to use the fanart.tv webservice API but have a couple of issues. I am using Json.Net (Newtonsoft.Json) and with other web-services I have de-serialized the JSON reponses into C# objects directly. The issue here is that the element names are changing. Eg. if I search for artist thumbs for Metallica you get {"Metallica":{"mbid_id":"65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab","artistthumb": [{"id":"36181","url":"http://assets.fanart.tv/fanart/music/65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab

ServiceStack.Text does not serialize my object as expected

余生长醉 提交于 2019-12-12 16:28:41
问题 I'm trying to compare performance results of serialization / deserialization using Newtonsoft.Json and ServiceStack.Text libraries. I have a large class which is named Application and I'm using an instance of this class for these operations. For the same instance NewtonSoft.Json works fine and gives me the following output: { "$id": "1", "_expiryDate": { "$id": "2", "_underlyingValue": null, "_isModified": false, "_isTrimmed": false }, "_number": { "$id": "3", "_underlyingValue": 700771, "

Force JObject to serialzie date in “dd-mm-yyyy” format

牧云@^-^@ 提交于 2019-12-12 16:19:01
问题 public Guid AddJobs(JObject parametrs) { dynamic jsonParameters = parametrs; JobViewModel job = jsonParameters.Job.ToObject<JobViewModel>(); } Above is my code. I am trying to deserialize this model using above method. The problem is that it keeps on giving me exception that date is not in correct format as it is not expecting "dd-mm-yyyy". Please Help me out in this. 回答1: Here's two approaches: 1.Set the format directly on the serializer. It will throw an exception on incorrect values. var

Parse nested JSON with json.net in Visual Basic

心已入冬 提交于 2019-12-12 16:08:01
问题 I have nested JSON strings that i would like to parse out the appropriate values from much like below. As i am learning by doing I am struggling a little bit, I have the first part working in that I can parse out single JSON strings, and return the appropriate value using code example 1 below, however i am stuck with a JSON string that is problematic in that it is nested, so the same approach won't work { "jsonrpc":"2.0", "method":"Player.OnPause", "params":{ "data": { "item": { "id":29,

Newtonsoft.Json Cannot create and populate list type

我只是一个虾纸丫 提交于 2019-12-12 15:49:13
问题 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

How to generate XML from JSON with parent node of array items

霸气de小男生 提交于 2019-12-12 15:14:12
问题 I am trying to create an XML document that closely conforms to a C# object graph and its JSON representation, but am having difficulty with the list representation in the XML. Given this graph public class X { public List<A> Aa { get; set; } } public class A { public int B; public bool C; } I took the JSON from the above, and tried converting it a couple of ways: var json = @"{""Aa"":[{""B"":186,""C"":true},{""B"":9,""C"":false},{""B"":182,""C"":true}]}"; var xml = JsonConvert

C# Newtonsoft deserialize JSON array

岁酱吖の 提交于 2019-12-12 14:53:28
问题 I'm trying to deserialize an array using Newtonsoft so i can display files from a cloud based server in a listbox but i always end up getting this error no matter what i try: Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: [. Path '[0].priv', line 4, position 15.' Thisis an example try to deserialize: [ { "code": 200, "priv": [ { "file": "file.txt", "ext": "txt", "size": "104.86" }, { "file": "file2.exe", "ext": "exe", "size": "173.74" }, ], "pub":

JSON.net serializes a json array into a JArray when the destination is an object. How can I change that?

走远了吗. 提交于 2019-12-12 14:20:27
问题 I have a single level json that I want to deserialize into a Dictionary<string,object> using Json.Net . The dictionary's value can be a primitive, string or a (primitive\string) array. The deserialization knows how to handle primitives and strings, but when it gets to an array of primitives it deserializes it into a JArray (instead of a primitive array). Here's a small code example of what I mean: string jsonStr = @"{""obj"": 7, ""arr"": ['1','2','3']}"; Dictionary<string, object> dict =