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

前端 未结 3 1724
天命终不由人
天命终不由人 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 dictionary = new Dictionary();
     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\"}}}";
    }
    

提交回复
热议问题