json.net

Why is my JSON Response not being Deserialized on my Client into the same Generic List Type as the Origin IEnumerable Type on the Web API server?

一曲冷凌霜 提交于 2020-01-06 01:30:06
问题 If I understand Ufuk Hacıoğulları here I can simplify this code: using (var webResponse = (HttpWebResponse)webRequest.GetResponse()) { if (webResponse.StatusCode == HttpStatusCode.OK) { var reader = new StreamReader(webResponse.GetResponseStream()); string s = reader.ReadToEnd(); var arr = JsonConvert.DeserializeObject<JArray>(s); if (arr.Count <= 0) break; foreach (JObject obj in arr) { id = obj.Value<int?>("Id") ?? 0; var _deptId = obj.Value<int?>("deptId") ?? 0; var _subdeptId = obj.Value

Creating JObject with JObject.FromObject ignores DateFormatString

☆樱花仙子☆ 提交于 2020-01-05 09:05:52
问题 I am trying to create a Newtonsoft JObject with a custom DateFormatSting ("yyyy-MM-ddTHH:mm:ssZ") using JOjbect.FromObject and I think there is a bug. My code is: JObject jBytes = JObject.FromObject(myObject, MyJsonSerializer); Here, JObject.FromObject seems to ignore the DateFormatString in my custom JsonSerializer. I have a workaround, but still curious if I am doing something wrong, or if anyone else has seen this? (workaround) JObject jBytes = Object.Parse(JsonConvert.SerializeObject

Force decimal type in class definition during serialization

三世轮回 提交于 2020-01-05 08:55:09
问题 I have a class. One of the members of the class is having subtypes as Decimal. Default Json serializer (not directly used. Used by some no-sql libraries for read/write), converts these Decimal values as Double. Since, external library internally serializes the object, I'm looking for a way to add JsonProperty such that it forces FloatParseHandling.Decimal flag. I have found this article. But, there we are specifically setting the flag during serialization which I don't have access to. class

Error during serialization using the JSON JavaScriptSerializer. Should be using the JSON.NET JsonSerializer

早过忘川 提交于 2020-01-05 08:41:56
问题 I've added Newton Soft to the mix and am using this blog as a template: http://wingkaiwan.com/2012/12/28/replacing-mvc-javascriptserializer-with-json-net-jsonserializer/ I now have: public class BaseJsonController : BaseController { protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) { return new JsonNetResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior }; }

Dynamic json serialization filtering via reflection

不问归期 提交于 2020-01-05 08:31:38
问题 I want to create a dynamic contract resolver for json.net which will exclude fields in runtime. The idea is to pass into constructor something which will exclude certain fields inside CreateProperties override. So far i came up with passing PropertyInfo[] which relies on Json / Class properties name equality which is not good in long run ( ie. i want to override json property name to something shorter ). Another issue with solution is that i need to pass PropertyInfo[] which is not intuitive

How do I represent this complex json document as a C# object?

浪子不回头ぞ 提交于 2020-01-05 08:18:29
问题 I would like to create a C# base object class that represents the Json document below(which is a questionnaire form). I can do the first three three lines easily but how do I do the nested ones from "FormBody": { } on wards. I have seen where guys use public IDictionary<string, object> FormBody {get; set;} . Can I pass this in to Json.Net and let it populate the object class for me? Or are there other ways? { "FormTitle": "Product 27", "FormId": "CCC12345", "Type": "forms", "FormBody": {

Parsing JSON into C# Object - Get Properties Dynamically

为君一笑 提交于 2020-01-05 07:18:28
问题 (Skip to bolded part for one-sentence tl;dr:) I have the JSON object below. Before looking at it, note that: The list of currency pairs like BTC_AMP goes on forever, I cut it off for the sake of example BTC_AMP appears to be a NAMED OBJECT containing some fields. { "BTC_AMP": { "asks": [ [ "0.00007400", 5 ] ], "bids": [ [ "0.00007359", 163.59313969 ] ], "isFrozen": "0", "seq": 38044678 }, "BTC_ARDR": { "asks": [ [ "0.00003933", 7160.61031389 ] ], "bids": [ [ "0.00003912", 1091.21852308 ] ],

Convert empty strings to null with Json.Net [closed]

﹥>﹥吖頭↗ 提交于 2020-01-05 07:08:12
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I'm having trouble finding a way to automatically deserialize (server side) all EmptyOrWhiteSpace strings to null . Json.Net by default simply assigns the value to the object property, and I need to verify string by string whether it is empty or white space, and then set it to

c# JsonConverter Attribute with DependencyInjection

雨燕双飞 提交于 2020-01-05 04:58:05
问题 I'm using JsonConverter to resolve Interfaces in asp core. Here is my JsonConverter-Impl. public class InterfaceJsonConverter<T> : Newtonsoft.Json.JsonConverter { public InterfaceJsonConverter(Type[] types, ITypeRepository typeRepository=null){ ... } } and this is how I call it [JsonConverter(typeof(InterfaceJsonConverter<ISomeInterface>), new object[] {new Type[]{ typeof(MyCustomType) }})] public interface ISomeInterface{ ... } Basically this works well when I remove my optional Parameter

C# Deserialize JSON string into object using Newtonsoft.Json

元气小坏坏 提交于 2020-01-05 04:04:19
问题 I am trying to convert a json string to an object using Newtonsoft.json, but I am having some problems with the following conversion. I wonder if some one can explain this. thanks. AddFaceResponse ir = JsonConvert.DeserializeObject<AddFaceResponse>(responseContentStr); this is the json string responseContentStr [{ "faceId": "1fe48282-a3b0-47d1-8fa8-67c4fac3d984", "faceRectangle": { "top": 80, "left": 50, "width": 147, "height": 147 } }] This is my model object. public class AddFaceResponse {