JSON.NET JObject key comparison case-insensitive

后端 未结 2 1675
醉话见心
醉话见心 2020-12-07 00:19

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

相关标签:
2条回答
  • 2020-12-07 01:02

    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();
    
    0 讨论(0)
  • 2020-12-07 01:03

    This should work:

    string json = @"{UPPER: 'value'}";
    JObject o = JObject.Parse(json);
    var value = o.GetValue("upper", StringComparison.OrdinalIgnoreCase)?.Value<string>();
    
    0 讨论(0)
提交回复
热议问题