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();
Refer to this answer that this is wanted according the JSON-RPC spec (keys are case sensitive!).
Json does case sensitive JSON deserialization. Case sensitivity comes into play when a JSON string is being deserialized into an object.
The JToken hierarchy looks like this: JToken - abstract base class JContainer - abstract base class of JTokens that can contain other JTokens JArray - represents a JSON array (contains an ordered list of JTokens) JObject - represents a JSON object (contains a collection of JProperties) JProperty - represents a JSON ...
JToken is the abstract base class of JObject , JArray , JProperty , and JValue , which represent pieces of JSON data after they have been parsed. JsonToken is an enum that is used by JsonReader and JsonWriter to indicate which type of token is being read or written.
This should work:
var json = @"{UPPER: 'value'}"; var jObj = JObject.Parse(json); var upper = jObj.GetValue("upper", StringComparison.OrdinalIgnoreCase)?.Value<string>(); Console.WriteLine(upper); // value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With