I am trying to bind a drop-down with these lists in my View. Not able to convert the below JSON collections to LISTS.
My JSON string is in this format:
["AppTypes",
[
{
"AppTypeID": "5136",
"AppType": "ABC"
},
{
"AppTypeID": "dca6",
"AppType": "MNO"
},
{
"AppTypeID": "d8de",
"AppType": "PQR"
}
],
"CompTypes",
[
{
"CompTypeID": "0425",
"CompType": "STU"
},
{
"CompTypeID": "0426",
"CompType": "EDC"
},
{
"CompTypeID": "0444",
"CompType": "PLM"
}
]
]
I am trying to deserialize the code, but not getting through. My Class,
public class DAL
{
public DAL() { }
public CompList CompList { get; set; }
}
public class CompList
{
public CompList()
{
AppTypes = new List<AppTypes>();
CompType = new List<CompTypes>();
}
public List<AppTypes> AppTypes;
public List<ComTypes> CompType;
}
public class AppTypes
{
public Guid AppTypeID { get; set; }
public string AppType { get; set; }
}
public class CompTypes
{
public Guid CompTypeID { get; set; }
public string CompType { get; set; }
}
public class JSONSerializer
{
public static T ConvertFromJSON<T>(String json)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Deserialize<T>(json);
}
}
Controller Code:
dynamic obj = JSONSerializer.ConvertFromJSON<DAL>(JsonData);
Not able to get where I am going wrong. Any help would be really appreciated.
Deserializecall. You will need to use a JsonReader or something.JavaScriptSerializersince it's WAAAYY more performant and more flexible.AppTypesandCompTypearen't properties (no getter/settters). If the json was valid, it would be bound correctly by theDefaultModelBinderanyway so all this is unnecessary.{AppTypes: [ { "AppTypeID": "5136", "AppType": "ABC" }, {..} ], CompType: [ {..}, {..} ] }, and you don't have{ get; set; }onAppTypesandCompType