How can I deserialize JSON to a simple Dictionary in ASP.NET?

前端 未结 21 2758
粉色の甜心
粉色の甜心 2020-11-21 06:33

I have a simple key/value list in JSON being sent back to ASP.NET via POST. Example:

{ \"key1\": \"value1\", \"key2\": \"value2\"}

21条回答
  •  攒了一身酷
    2020-11-21 07:00

    A bit late to the game, but non of the above solutions pointed me in the direction of a pure and simple .NET, no json.net solution. So here it is, ended up being very simple. Below a full running example of how it is done with standard .NET Json serialization, the example has dictionary both in the root object and in the child objects.

    The golden bullet is this cat, parse the settings as second parameter to the serializer:

    DataContractJsonSerializerSettings settings =
                           new DataContractJsonSerializerSettings();
                        settings.UseSimpleDictionaryFormat = true;
    

    Full code below:

    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    
        namespace Kipon.dk
        {
            public class JsonTest
            {
                public const string EXAMPLE = @"{
                    ""id"": ""some id"",
                    ""children"": {
                    ""f1"": {
                        ""name"": ""name 1"",
                        ""subs"": {
                        ""1"": { ""name"": ""first sub"" },
                        ""2"": { ""name"": ""second sub"" }
                        }
                    },
                    ""f2"": {
                        ""name"": ""name 2"",
                        ""subs"": {
                        ""37"": { ""name"":  ""is 37 in key""}
                        }
                    }
                    }
                }
                ";
    
                [DataContract]
                public class Root
                {
                    [DataMember(Name ="id")]
                    public string Id { get; set; }
    
                    [DataMember(Name = "children")]
                    public Dictionary Children { get; set; }
                }
    
                [DataContract]
                public class Child
                {
                    [DataMember(Name = "name")]
                    public string Name { get; set; }
    
                    [DataMember(Name = "subs")]
                    public Dictionary Subs { get; set; }
                }
    
                [DataContract]
                public class Sub
                {
                    [DataMember(Name = "name")]
                    public string Name { get; set; }
                }
    
                public static void Test()
                {
                    var array = System.Text.Encoding.UTF8.GetBytes(EXAMPLE);
                    using (var mem = new System.IO.MemoryStream(array))
                    {
                        mem.Seek(0, System.IO.SeekOrigin.Begin);
                        DataContractJsonSerializerSettings settings =
                           new DataContractJsonSerializerSettings();
                        settings.UseSimpleDictionaryFormat = true;
    
                        var ser = new DataContractJsonSerializer(typeof(Root), settings);
                        var data = (Root)ser.ReadObject(mem);
                        Console.WriteLine(data.Id);
                        foreach (var childKey in data.Children.Keys)
                        {
                            var child = data.Children[childKey];
                            Console.WriteLine(" Child: " + childKey + " " + child.Name);
                            foreach (var subKey in child.Subs.Keys)
                            {
                                var sub = child.Subs[subKey];
                                Console.WriteLine("   Sub: " + subKey + " " + sub.Name);
                            }
                        }
                    }
                }
            }
        }
    

提交回复
热议问题