json.net

IHttpActionResult with JSON string

╄→гoц情女王★ 提交于 2019-12-04 09:10:33
I have a method that originally returned an HttpResponseMessage and I'd like to convert this to return IHttpActionResult . My problem is the current code is using JSON.Net to serialize a complex generic tree structure, which it does well using a custom JsonConverter I wrote (the code is working fine). Here's what it returns: string json = NodeToJson(personNode); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(json, Encoding.UTF8, "application/json"); return response; The NodeToJson method is where the custom converter comes into

JSON Deserialization with an array of polymorphic objects

社会主义新天地 提交于 2019-12-04 08:28:27
问题 I'm having a problem with JSON Deserialization involving an array of polymorphic objects. I've tried the solutions for serialization documented here and here which work great for serialization, but both blow up on deserialization. My class structure is as follows: IDable [DataContract(IsReference=true)] public abstract class IDable<T> { [DataMember] public T ID { get; set; } } Observation Group [DataContract(IsReference=true)] [KnownType(typeof(DescriptiveObservation))] [KnownType(typeof

JSON.NET: Deserializing part of a JSON object to a dictionary

谁说我不能喝 提交于 2019-12-04 08:17:10
I have JSON like this: { "Property":"Blah blah", "Dictionary": { "Key1" : "Value1", "Key2" : "Value2", "Key3" : "Value3" } } I want to extract the "Dictionary" object as a Dictionary (so it'd be like Key1 => Value1, etc.). If I just had the "Dictionary" object directly, I could use: JsonConvert.DeserializeObject<Dictionary<string, string>> What's the best way to get just the Dictionary property as a Dictionary? Thanks in advance! Tim Took me a little while to figure out, but I just didn't feel great about using string parsing or regexes to get at the inner JSON that I want. Simple enough; I

C# deserialize Json unknown keys

妖精的绣舞 提交于 2019-12-04 07:57:05
I have this JSON and i have to deserialize it: { "homepage": "http://files.minecraftforge.net/maven/net/minecraftforge/forge/", "promos": { "1.10-latest": "12.18.0.2000", "1.10.2-latest": "12.18.1.2014", "1.10.2-recommended": "12.18.1.2011", "1.5.2-latest": "7.8.1.738", "1.5.2-recommended": "7.8.1.737", "1.6.1-latest": "8.9.0.775", "1.6.2-latest": "9.10.1.871", "1.6.2-recommended": "9.10.1.871", "1.6.3-latest": "9.11.0.878", "1.6.4-latest": "9.11.1.1345", "1.6.4-recommended": "9.11.1.1345", "1.7.10-latest": "10.13.4.1614", "1.7.10-latest-1.7.10": "10.13.2.1343", "1.7.10-recommended": "10.13.4

Method not found: 'Newtonsoft.Json.JsonSerializerSettings Microsoft.AspNet.Mvc.MvcJsonOptions.get_SerializerSettings()'

半城伤御伤魂 提交于 2019-12-04 07:47:12
Locally my project runs fine but when I deploy on Azure using a web app, I get the following error when it starts: MissingMethodException: Method not found: 'Newtonsoft.Json.JsonSerializerSettings Microsoft.AspNet.Mvc.Formatters.JsonOutputFormatter.get_SerializerSettings()'. SmartAdmin.Startup.<>c.b__13_7(MvcOptions options) I've tried this: services.AddMvc(options => { options.Filters.Add(new UserPreferencesLoaderAtrribute()); var jsonFormatter = (JsonOutputFormatter)options.OutputFormatters.FirstOrDefault(f => f is JsonOutputFormatter); if (jsonFormatter != null) { jsonFormatter

json.NET parsing issue with twitter API data

ε祈祈猫儿з 提交于 2019-12-04 07:35:34
I have been using json.NET successfully in projects for some time now without any problems. Last night I ran into my first case where json.NET crashed trying to parse json data returned from what should be a reliable source: the twitter API. Specifically, this code causes the error: string sCmdStr = String.Format("https://api.twitter.com/1/users/lookup.json?screen_name={0}", sParam); string strJson = _oauth.APIWebRequest("GET", sCmdStr, null); JObject jsonDat = JObject.Parse(strJson); In my case the sParam string contained about 25 twitter numeric Ids. The twitter API call succeeded, but the

Json.NET says “operation may destabilize the runtime” under .NET 4, but not under .NET 3.5

笑着哭i 提交于 2019-12-04 06:59:34
This code: namespace ConsoleApplication3 { class Program { static void Main(string[] args) { var client = new WebClient(); client.Headers.Add("User-Agent", "Nobody"); var response = client.DownloadString(new Uri("http://www.hanselman.com/smallestdotnet/json.ashx")); var j = JsonConvert.DeserializeObject<SmallestDotNetThing>(response); } public class SmallestDotNetThing { public DotNetVersion latestVersion { get; set; } public List<DotNetVersion> allVersions { get; set; } public List<DotNetVersion> downloadableVersions { get; set; } } public class DotNetVersion { public int major { get; set; }

How to serialize static properties in JSON.NET without adding [JsonProperty] attribute

倾然丶 夕夏残阳落幕 提交于 2019-12-04 06:56:13
Is it possible to serialize static properties with JSON.NET without adding [JsonProperty] attribute to each property. Example class: public class Settings { public static int IntSetting { get; set; } public static string StrSetting { get; set; } static Settings() { IntSetting = 5; StrSetting = "Test str"; } } Expected result: { "IntSetting": 5, "StrSetting": "Test str" } Default behavior skips static properties: var x = JsonConvert.SerializeObject(new Settings(), Formatting.Indented); You can do this with a custom contract resolver. Specifically you need to subclass DefaultContractResolver and

Json.NET - deserialize directly from a stream to a dynamic?

蓝咒 提交于 2019-12-04 06:53:42
With a little help from the performance tips in the Json.NET docs, I put together a method for downloading/deserializing JSON from a remote resource: public async Task<T> GetJsonAsync<T>(string url) { using (var stream = await new HttpClient().GetStreamAsync(url)) { using (var sr = new StreamReader(stream)) { using (var jr = new JsonTextReader(sr)) { return new JsonSerializer().Deserialize<T>(jr); } } } } I'd like to have a non-generic version that returns a dynamic . Calling the above method with GetJsonAsync<dynamic>(url) works, until you try to access a dynamic property on the result, at

Json.net deseralize to a list of objects in c# .net 2.0

↘锁芯ラ 提交于 2019-12-04 06:39:25
I'm trying to deseralize some json into a collection (list), but I'm not sure which method will return a list of objects, or do I have to loop through something and copy it to my own list? Can anyone tell me the syntax or method I should use for this. I've created my object with some properties, so it's ready to be used to hold the data. (title,url,description) I've tried this, but it doesn't seem quite right List<newsItem> test = (List<newsItem>)JsonConvert.DeserializeObject(Fulltext); James Newton-King Did you try looking at the help? http://james.newtonking.com/json/help/?topic=html