json.net

How to prevent Json.NET converting enum to string?

落花浮王杯 提交于 2019-12-22 09:27:45
问题 The following class public class RequestSections : RequestBase { public RequestSections(Command c, Dictionary<SectionIdentifier, BigInteger> v) : base(c) { VERSIONS = v; } public Dictionary<SectionIdentifier, BigInteger> VERSIONS { get; set; } } is serialized to JSON using JSON.NET and producing the following JSON output: { "VERSIONS": { "Photos": 901, "Music": 902 }, "CMD": 43 } The problem is that SectionIdentifier is enum but JSON.NET converts them to string. public enum SectionIdentifier

JSON.NET Parse with JObject, JToken and JArray

[亡魂溺海] 提交于 2019-12-22 09:13:57
问题 I have a json string that i'm trying to parse with JSON.net, i want to loop and use the names in the komponent array. This is my json string: {"Name": "Service","jsonTEMPLATE": "{"komponent": [{"name": "aa"}, {"name": "bb"}]}"} This is my code using JSON.net JObject o = JObject.Parse(serviceData); JToken j = (JToken)o.SelectToken("jsonTEMPLATE"); JArray a = (JArray)j.SelectToken("komponent"); foreach (JObject obj in a) { //Do something } i get null from (JArray)j.SelectToken("komponent");

Sanitizing Input with JsonConvert.SerializeObject in MVC4?

僤鯓⒐⒋嵵緔 提交于 2019-12-22 08:59:10
问题 Long story short, I'm trying to get the output from JsonConvert.SerializeObject to be sanitized without having to modify the contents of the saved data. I'm working on an app that has the following markup in the view: <textarea data-bind="value: aboutMe"></textarea> If I save the following text, I run into problems: <script type="text/javascript">alert("hey")</script> The error I get in FF: The relevant part of the offending rendered text: $(document).ready(ko.applyBindings(new MyProfileVm({

How to deserialize with JSON.NET?

拥有回忆 提交于 2019-12-22 08:58:02
问题 How do I setup Newtonsoft Json.net to deserialize this text into a .NET object? [ [ "US\/Hawaii", "GMT-10:00 - Hawaii" ], [ "US\/Alaska", "GMT-09:00 - Alaska" ], ] For bonus points, what is this kind of structure called in Json. I tried looking for anonymous objects, but didn't have any luck. 回答1: JSON.Net uses JArray to allow these to be parsed - see: Deserializing variable Type JSON array using DataContractJsonSerializer (the questions is about DataContract - but the answer is about JSON

Json.Net messes up timezones for DateTimeOffset when serializing

房东的猫 提交于 2019-12-22 08:33:59
问题 I have looked at a lot of related questions but none of them seem to be working for me. I'm trying to serialize everything in UTC. Here's my code: class Class1 { static void Main() { Class2 foo = new Class2(); JObject json = JObject.Parse(JsonConvert.SerializeObject(foo, new JsonSerializerSettings() { DateParseHandling = DateParseHandling.DateTimeOffset, DateFormatHandling = DateFormatHandling.IsoDateFormat, DateTimeZoneHandling = DateTimeZoneHandling.Utc })); Console.WriteLine(json.ToString(

Deserialized Object Has All Values Set to Null

﹥>﹥吖頭↗ 提交于 2019-12-22 08:03:29
问题 I'm trying to deserialize JSON into a custom object but all my properties are set to null and not sure what's going on. Does anyone see anything wrong? JSON Example { "Keys": [ { "RegistrationKey": "asdfasdfa", "ValidationStatus": "Valid", "ValidationDescription": null, "Properties": [ { "Key": "Guid", "Value": "i0asd23165323sdfs68661358" } ] } ] } Here is my Code, where strResponseValid is the JSON above. Keys myDeserializedObjValid = (Keys)JsonConvert.DeserializeObject(strResponseValid,

Deserialize json objects and transform inner objects to a string value?

拥有回忆 提交于 2019-12-22 07:59:09
问题 I have a webservice returning json data. I have no control over the server side generated json. I'm deserializing json like this: JsonConvert.DeserializeObject<OuterObject>(jsonString); The problem is there are inner objects embedded (with a whole lot of more nested inner objects). I have no interest in modeling them in my application. The json-data goes like this: { id : "xyz", name : "Some Object", properties : { prop_1 : "foo", prop_2 : "bar" }, inner_object : { id : "abc$1", name : "Inner

How to exclude specific type from json serialization

 ̄綄美尐妖づ 提交于 2019-12-22 07:08:11
问题 I am logging all requests to my WCF web services, including the arguments, to the database. This is the way I do it: create a class WcfMethodEntry which derives from PostSharp's aspect OnMethodBoundaryAspect, annotate all WCF methods with WcfMethodEntry attribute, in the WcfMethodEntry I serialize the method arguments to json with the JsonConvert.SerializeObject method and save it to the database. This works ok, but sometimes the arguments are quite large, for example a custom class with a

Handling JSON single object and array

十年热恋 提交于 2019-12-22 04:47:09
问题 I'm using Newtonsoft.Json to work with some JSON data that is being returned to me. Depending on what I request I can either get back something that looks like: { "TotalRecords":2, "Result": [ { "Id":24379, "AccountName":"foo" }, { "Id":37209, "AccountName":"bar" } ], "ResponseCode":0, "Status":"OK", "Error":"None" } or { "Result": { "Id":24379, "AccountName":"foo" }, "ResponseCode":0, "Status":"OK", "Error":"None" } So sometimes "Result" is an array of Results or "Result" could be a single

Serializing a custom subclass of NameValueCollection with Json.Net

孤者浪人 提交于 2019-12-22 04:34:10
问题 I have the following class I am unsuccessfully attempting to serialize to Json. class HL7 : NameValueCollection { public List<HL7> Children { get; set; } public HL7() { Children = new List<HL7>(); } } I have created the object like so and added data to it: HL7 hl7 = new HL7(); hl7.Add("a", "123"); hl7.Add("b", "456"); hl7.Children.Add(new HL7()); hl7.Children[0].Add("c", "123"); hl7.Children[0].Add("d", "456"); When I call JsonConvert.SerializeObject(hl7) I receive ["a","b"] I was expecting