Parse JSON in C#

[Update] I’ve just realized why you weren’t receiving results back… you have a missing line in your Deserialize method. You were forgetting to assign the results to your obj : public static T Deserialize<T>(string json) { using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); return (T)serializer.ReadObject(ms); } } Also, just for … Read more

Could not load file or assembly ‘Newtonsoft.Json’ or one of its dependencies. Manifest definition does not match the assembly reference

To solve this, I ensured all my projects used the same version by running the following command and checking the results: update-package Newtonsoft.Json -reinstall And, lastly I removed the following from my web.config: <dependentAssembly> <assemblyIdentity name=”Newtonsoft.Json” publicKeyToken=”30ad4fe6b2a6aeed” culture=”neutral” /> <bindingRedirect oldVersion=”0.0.0.0-6.0.0.0″ newVersion=”6.0.0.0″ /> </dependentAssembly> If you want to ensure all your Newtonsoft.Json packages are the … Read more

Could not load file or assembly ‘Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’

In package manager console execute: Update-Package –reinstall Newtonsoft.Json. UPDATE I originally posted this as a comment but as @OwenBlacker suggested I’ll just put it here: If you still get an error after doing this, then what worked for me eventually is that I deleted Json.Net’s <dependentAssembly> section from my .config file. Reinstall brings it back … Read more

How can I return camelCase JSON serialized by JSON.NET from ASP.NET MVC controller methods?

or, simply put: JsonConvert.SerializeObject( <YOUR OBJECT>, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); For instance: return new ContentResult { ContentType = “application/json”, Content = JsonConvert.SerializeObject(new { content = result, rows = dto }, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }), ContentEncoding = Encoding.UTF8 };

How to convert JSON to XML or XML to JSON?

Yes. Using the JsonConvert class which contains helper methods for this precise purpose: // To convert an XML node contained in string xml into a JSON string XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string jsonText = JsonConvert.SerializeXmlNode(doc); // To convert JSON text contained in string json into an XML node XmlDocument doc = JsonConvert.DeserializeXmlNode(json); Documentation … Read more

Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings. JSON The JSON string below is a simple response from an HTTP API call, and it defines two properties: Id and Name. {“Id”: 1, “Name”: “biofractal”} C# Use JsonConvert.DeserializeObject<dynamic>() to deserialize … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)