JSON.NET JToken Keys Are Case Sensitive?

前端 未结 2 547
谎友^
谎友^ 2021-01-12 09:46

I\'m having to perform some custom deserialization with JSON.NET and I just found that it\'s treating the key values in a JToken as case sensitive. Here\'s some code:

<
相关标签:
2条回答
  • 2021-01-12 09:56

    Convert JToken to JObject and use TryGetValue method of JObject in which you can specify String Comparision.

     var jObject = JToken.Load(reader) as JObject;
     JToken version;
     jObject.TryGetValue("version", StringComparison.OrdinalIgnoreCase, out version);
    
    0 讨论(0)
  • 2021-01-12 10:08

    You can cast JToken to JObject and do this:

    string ver = ((JObject)token).GetValue("version", StringComparison.OrdinalIgnoreCase)?.Value<string>();
    
    0 讨论(0)
提交回复
热议问题