使用 JavaScriptSerializer.DeserializeObject 如何取回使用不区分大小写的字符串比较器的字典?

发布于 2024-12-19 08:53:58 字数 291 浏览 2 评论 0原文

我有一些需要反序列化的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧夏天 2024-12-26 08:53:58

只需创建一个新的不区分大小写的字典并用当前的字典填充它即可。

var jsonObject = serializer.DeserializeObject(line) as Dictionary<string, object>;
var caseInsensitiveDictionary = new Dictionary<string, object>(jsonObject, StringComparer.OrdinalIgnoreCase);

[更新]
测试代码:

    Stopwatch stop1 = new Stopwatch();
    Stopwatch stop2 = new Stopwatch();

    //do test 100 000 times
    for (int j = 0; j < 100000; j++)
    {
        //generate fake data
        //object with 50 properties
        StringBuilder json = new StringBuilder();
        json.Append('{');
        for (int i = 0; i < 100; i++)
        {
            json.Append(String.Format("prop{0}:'val{0}',", i));
        }
        json.Length = json.Length - 1;
        json.Append('}');

        var line = json.ToString();

        stop1.Start();
        var serializer = new JavaScriptSerializer();
        var jsonObject = serializer.DeserializeObject(line) as Dictionary<string, object>;
        stop1.Stop();

        stop2.Start();
        var caseInsensitiveDictionary = new Dictionary<string, object>(jsonObject, StringComparer.OrdinalIgnoreCase);
        stop2.Stop();
    }

    Console.WriteLine(stop1.Elapsed);
    Console.WriteLine(stop2.Elapsed);
    Console.Read();

结果是:

反序列化时间:1分21秒

字典创建时间:3秒

所以,主要问题是反序列化。词典创建约为 4%

Just create a new case insensitive dictionary and populate it with the current one.

var jsonObject = serializer.DeserializeObject(line) as Dictionary<string, object>;
var caseInsensitiveDictionary = new Dictionary<string, object>(jsonObject, StringComparer.OrdinalIgnoreCase);

[UPDATE]
Test code:

    Stopwatch stop1 = new Stopwatch();
    Stopwatch stop2 = new Stopwatch();

    //do test 100 000 times
    for (int j = 0; j < 100000; j++)
    {
        //generate fake data
        //object with 50 properties
        StringBuilder json = new StringBuilder();
        json.Append('{');
        for (int i = 0; i < 100; i++)
        {
            json.Append(String.Format("prop{0}:'val{0}',", i));
        }
        json.Length = json.Length - 1;
        json.Append('}');

        var line = json.ToString();

        stop1.Start();
        var serializer = new JavaScriptSerializer();
        var jsonObject = serializer.DeserializeObject(line) as Dictionary<string, object>;
        stop1.Stop();

        stop2.Start();
        var caseInsensitiveDictionary = new Dictionary<string, object>(jsonObject, StringComparer.OrdinalIgnoreCase);
        stop2.Stop();
    }

    Console.WriteLine(stop1.Elapsed);
    Console.WriteLine(stop2.Elapsed);
    Console.Read();

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%

江湖正好 2024-12-26 08:53:58

我建议创建一个继承自 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文