JSON.NET JObject key comparison case-insensitive

c#, json, json.net

Solution

c# allows you to use dictionaries with keys that are case insensitive, so a workaround I've used is to convert the JObject to a dictionary with `StringComparer.CurrentCultureIgnoreCase` set, like so:

JObject json = (JObject)JsonConvert.DeserializeObject(ptString);
Dictionary<string, object> d = new Dictionary<string, object>(json.ToObject<IDictionary<string, object>>(), StringComparer.CurrentCultureIgnoreCase);

String f = d["FROM"].ToString();

Problem

I'm using Newtonsoft Json.net to parse the JSON string. I convert the string into the JObject. When access the value of the element by the key, I want to the comparison is case-insensitive. In the code below, I use "FROM" as the key. I want it returns string "1" at the line json["FROM"].ToString(). But it fails. Is it possible to make the code below work? ``` String ptString = "{from: 1, to: 3}"; var json = (JObject)JsonConvert.DeserializeObject(ptString); String f = json["FROM"].ToString(); ```

Original source

Related problems