. Net converts a Json string to a specified object.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.