Using dynamic in. net 4 to convert a json string into an object.

Source: Internet
Author: User

Added support for weak types in. net 4. It provides a way to exchange data with weak objects. We often encounter the scenario of converting a json string into an object, although we can use

JavaScriptSerializer orDataContractJsonSerializer

However, some corresponding entity classes are still required, which is very troublesome. I checked the information online and found another implementation method:

Usage:

 json =  j =  len = obj1 = j[ name = j[].name;

 

Note that the dynamic type is used here. Here, the power of the weak type is highlighted.

Let's look at the specific implementation class:

                                                  dynamic FromJson(=  JavaScriptConverter[] { = jss.Deserialize(jsonStr, ())     Deserialize(IDictionary<, > (dictionary ==   ArgumentNullException( (type == (    IDictionary<, > Serialize(   IEnumerable<Type> {   ReadOnlyCollection<Type>( List<Type>( Type[] { (  IDictionary<, > Dictionary { ;  DynamicJsonObject(IDictionary<, >.Dictionary =   TryGetMember(GetMemberBinder binder,  =  (result  IDictionary<, >=  DynamicJsonObject(result  IDictionary<, >  (result  ArrayList && (result  ArrayList)  IDictionary<, >=  List<DynamicJsonObject>((result  ArrayList).ToArray().Select(x =>  DynamicJsonObject(x  IDictionary<, >  (result =  List<>((result  

 

Refer:

Http://www.codeproject.com/Articles/349646/Dynamic-JSON-parser

Http://msdn.microsoft.com/zh-cn/library/dd264736.aspx

Http://www.oschina.net/question/89964_45078

Http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4

Related Article

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.