使用 JavaScriptSerializer.DeserializeObject 如何取回使用不区分大小写的字符串比较器的字典?
我有一些需要反序列化的 JSON,所以我使用 JavaScriptSerializer.DeserializeObject,如下所示:
var jsonObject = serializer.DeserializeObject(line) as Dictionary<string, object>;
问题是返回的字典有一个区分大小写的键比较器,但我需要不区分大小写。有什么方法可以取回不区分大小写的字典吗?
编辑:我不想将数据复制到新结构,因为我有很多数据,这会很昂贵。
I have some JSON that I need to deserialize so I'm using JavaScriptSerializer.DeserializeObject like:
var jsonObject = serializer.DeserializeObject(line) as Dictionary<string, object>;
The problem is that the Dictionary that comes back has a case-sensitive key comparer, but I need case-insensitive. Is there some way to get back a Dictionary that is case-insensitive?
EDIT: I'd prefer not to copy the data to a new structure, since I have a lot of data and this will be costly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需创建一个新的不区分大小写的字典并用当前的字典填充它即可。
[更新]
测试代码:
结果是:
反序列化时间:1分21秒
字典创建时间:3秒
所以,主要问题是反序列化。词典创建约为 4%
Just create a new case insensitive dictionary and populate it with the current one.
[UPDATE]
Test code:
The result is:
Deserializtion time: 1 min 21 sec
Dictionary creation time: 3 sec
So, the main proble is deserializtion. Dictionary creation is about 4%
我建议创建一个继承自
Dictionary
的新类,并在此类的构造函数中分配不区分大小写的比较器。我不认为它可以与 JSON 序列化。I would recommend creating a new class that inherits from
Dictionary<string, object>
and in the constructor of this class, assign the case-insensitive comparer. I don't think it can be serialized to and from JSON.