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#.
{
"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"
}
}
}
If it was an array of methods, I could have easily serialized it using an array. But since that itself is a key value pair, I'm stuck.
I'm making an WebRequest to a particular api and getting the result as a json.
The code used to serialize the object is from https://msdn.microsoft.com/en-us/library/hh674188.aspx
The page you linked to describes how to use
DataContractJsonSerializer
to deserialize JSON. You can use that serializer to deserialize your"methods"
object as aDictionary
, provided you are using .Net 4.5 or later and also setDataContractJsonSerializerSettings.UseSimpleDictionaryFormat = true
. Having done this, you can define the following classes:And parse them as follows:
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.JSON.net does this: