Question: I use `JavaScriptSerializer` to serialize a json string to a specific Class

Here is the origin json string:

```
{
   "status":0,
   "data":[
          {
             "username":"yong6180212856@163.com",
             "password":"r2166745"
          }, 
          {
             "username":"xinnai1122139@163.com",
             "password":"f0341303"
          }
          ],
   "msg":"OK"
}

```

This is a structured responsed from a http server, and the `data` field will differ from different request. So in the `RetData` class I don't wan't to make a fixed type of `data` and just using `Object` then downcast to corresponding type when needed.

```
 class RetData {
    public int status {get;set;}
    public string msg {get;set;}
    public List<Object> data {get;set;}
 }

```

After using 

```
 RetData retData = new JavaScriptSerializer<RetData>(jsonStr);

```

Then I want to retrive the each password like this:

```
foreach (var item in retData.data){
   // some thing like this but I really don't know which type 
   // should I downcast to here.
   //                  |
   //                  V
   string password = ((??map??)item).getString("password"); 
   Console.WriteLine(password);
}

```

As you can see, what type should I downcast to, to retrieve the `password` flied, I am sort of familiar with Java but quite a newbie of C#, need some help.

Answer: 
There are lots of approaches here. The shortest path from where you are would be to change RetData to this:

```
public class RetData
{
    public int status { get; set; }
    public string msg { get; set; }
    public List<Dictionary<object,object>> data { get; set; }
}

```

Then read the data like this:

```
RetData retData = (RetData)new JavaScriptSerializer().Deserialize(jsonStr, typeof(RetData));

foreach (var item in retData.data)
{
    string password = item["username"].ToString();
    Console.WriteLine(password);
}

```

The limitation is that this will really only work with simple name/value pairs. The other approach would be to change to use Newtonsoft Json, instead of the Microsoft Json. This has a type called "JObject" which can handle truly any kind of Json data. Newtonsoft Json is available as a nuget package.

My comment about dynamic was wrong. I tried it and it turns out that the .NET serializer doesn't understand dynamic or ExpandoObject. :-(
Use the `dynamic`key word to define the type of `data`:

```
class RetData
{
    public int status { get; set; }
    public string msg { get; set; }
    public dynamic data { get; set; }
}

```

Note that this will take away intellisense and compiliation verifications, which means responsibility for checking the type is yours.

Alternatively, with a little bit more details about your design, you might be able to create a base data class, than inherit and implement it for your different data types.