Search code examples
c#asp.netasp.net-mvc-4javascriptserializer

How to deserialize JSON into a List<KeyValuePair<string,string>> set


I have some JSON data :-

{
    "mail":"mitch@domain.com",
    "givenName":"User",
    "sn":"Name",
    "uid":"mitch",
    "gecos":"User Name"
}

What I'm trying to do is de-serialize this into a List<KeyValuePair<string,string>>

I would normally do a dictionary, however some key's may be duplicated - this is the representation that is automatically generated by .NET when I pass a List<KeyValuePair<string,string>> object into the System.Web.Script.Serialization.JavaScriptSerializer class.

When I just plug the serialized object into the System.Web.Script.Serialization.JavaScriptDeserializer I get a empty response back.


Solution

  • From what I can see it should not be possible using the JavaScriptSerializer. The only way for customizing its behavior is by means of a JavaScriptConverter class, that will allow you to customize the serialization/deserialization process. Unfortunately both methods will pass an IDictionary for the properties, therefore the duplicated names are already merged. You might want to look into either a different format for your JSON or a different serialization library such as JSON.net which is way more customizable.