Requirement: 1. the data in Json format is returned at the front end: "[{name: 'hangsan ', age: '20'}, {name: 'lisi', age: '20'}]"
2. The json data needs to be converted to the corresponding Custom User class object in the background.
Solution:
// When defining a class, you must specify that it is serializable and inherit the List <Object> type to directly convert the List <Object> type to the List <User> type.
[Serializable]
Class User: List <Object>
{
Public string name {set; get ;}
Public string age {set; get ;}
}
Call method:
// Reference the corresponding namespace.
String json = "[{name: 'hangsan', age: '20'}, {name: 'lisi', age: '20'}]";
JavaScriptSerializer js = new JavaScriptSerializer ();
List <User> list = js. Deserialize <List <User> (json );
List <User> user_list = new List <User> ();
String field = "name ";
Foreach (User obj in list)
{// Loop list to check whether the property value is obtained correctly.
String name = obj. name;
String age = obj. age;
}