JSON.NET JObject键比较不区分大小写 [英] JSON.NET JObject key comparison case-insensitive
问题描述
我使用Newtonsoft Json.net解析JSON字符串。我转换串入JObject。当由键访问元素的值,我想的比较是不区分大小写。在下面的代码,我用,从为重点。我希望它在该行的JSON [FROM]返回字符串1。toString()方法。但它失败。 ?是否有可能使下面的工作代码
字符串ptString ={来自:1,3};
VAR JSON =(JObject)JsonConvert.DeserializeObject(ptString);
串f = json的[FROM]的ToString()。
C#允许你使用字典与区分键不敏感的,所以我用了一个解决方法是将JObject转换为用 StringComparer.CurrentCultureIgnoreCase
集,像这样一本字典:
JObject JSON =(JObject)JsonConvert.DeserializeObject(ptString);
&字典LT;字符串对象> D =新词典<字符串对象>(json.ToObject<&IDictionary的LT;字符串对象>>(),StringComparer.CurrentCultureIgnoreCase);
串f = D [FROM]的ToString()。
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();
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();
这篇关于JSON.NET JObject键比较不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!