2

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

6
  • what purpose you want the dictionary for? you could easily parse the json and get typed object in c# Commented Nov 18, 2015 at 6:38
  • By method name you mean "password.2", "ubikey.sms.1","tupas.test.1" ? Commented Nov 18, 2015 at 6:41
  • As an object also, when parsing the above json, it results in an object, containing a each method as a separate property. The number of methods can vary. I need a list of objects each containing the method name and their details. Commented Nov 18, 2015 at 6:42
  • 1
    What version of .net framework are you using? Commented Nov 18, 2015 at 6:44
  • @Alireza .NET Framework 4.5 Commented Nov 18, 2015 at 6:45

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, worked.. Decided to go with this method since I can avoid the JSON.net dependancy..
2

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);
        }


    }
}

Comments

0
 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\"}}}";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.