How to de-serialize a json object containing variable number of objects and get them as a key value collection in C#?

前端 未结 3 1695
天命终不由人
天命终不由人 2021-01-22 19:21

How can I deserialize the following JSON object and get a collection of Dictionary where the key(string) should be the method name and the object the details in C#.



        
相关标签:
3条回答
  • 2021-01-22 19:35
     dynamic results = JsonConvert.DeserializeObject(JsonString());
     Dictionary<string, object> dictionary = new Dictionary<string, object>();
     foreach (var o in results.methods)
     {
         dictionary[o.Name] = o.Value;
     }
    
    private static string JsonString()
    {
             return "{\"methods\": {\"password.2\": {\"title\": \"Password CustomerID\",\"type\": \"password\"},\"ubikey.sms.1\": {\"title\": \"SMS\",\"type\": \"stepup\",\"password\": \"password.2\",\"stepUp\": \"sms\"},\"tupas.test.1\": {\"title\": \"TUPAS Emulator\",\"type\": \"proxy\"}}}";
    }
    
    0 讨论(0)
  • 2021-01-22 19:44

    JSON.net does this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Newtonsoft.Json;
    
    namespace ConsoleApplication1
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                string json = @"{
    ""methods"": {
        ""password.2"": {
          ""title"": ""Password CustomerID"",
          ""type"": ""password""
        },
        ""ubikey.sms.1"": {
          ""title"": ""SMS"",
          ""type"": ""stepup"",
          ""password"": ""password.2"",
          ""stepUp"": ""sms""
        },
        ""tupas.test.1"": {
          ""title"": ""TUPAS Emulator"",
          ""type"": ""proxy""
        }
      }
    }";
    
                Dictionary<string, Dictionary<String,Dictionary<String,String>>> values = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<String,Dictionary<String,String>>>>(json);
            }
    
    
        }
    }
    
    0 讨论(0)
  • 2021-01-22 19:47

    The page you linked to describes how to use DataContractJsonSerializer to deserialize JSON. You can use that serializer to deserialize your "methods" object as a Dictionary, provided you are using .Net 4.5 or later and also set DataContractJsonSerializerSettings.UseSimpleDictionaryFormat = true. Having done this, you can define the following classes:

    public class Method
    {
        public string title { get; set; }
        public string type { get; set; }
        public string password { get; set; }
        public string stepUp { get; set; }
    }
    
    public class Response
    {
        public Dictionary<string, Method> methods { get; set; }
    }
    

    And parse them as follows:

            var settings = new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true };
            var jsonSerializer = new DataContractJsonSerializer(typeof(Response), settings);
            var jsonResponse = jsonSerializer.ReadObject(response.GetResponseStream()) as Response;
    

    If you are using an earlier version of .Net, you may need to use a different serializer such as Json.NET, since, in earlier versions, DataContractJsonSerializer serializes dictionaries as key/value pair arrays.

    0 讨论(0)
提交回复
热议问题