Parsing a JSON string with the dynamic tag

Source: Internet
Author: User

1 string " {\ "data\": {\ "ssotoken\": \ "70abd3d8a6654ff189c482fc4842468c\", \ "account\": \ "admin\", \ "usertype\": \ " Platformadmin\ ", \" realname\ ": \" Super Administrator \ ", \" sex\ ": 0,\" sexname\ ": \" male \ ", \" email\ ": \" [Email protected]\ ", \" mobile\ ": \ "15120757948\", \ "createddt\": \ "2013-08-16 00:00:00\", \ "updateddt\": \ "2014-12-10 00:00:00\"},\ "issuccess\": true} "

When the. Net program receives this Json string data, you will certainly think of using Newtonsoft.json to serialize (SerializeObject) and Deserialize (Deserializeobject) an object.

Examples of using serializeobject:

1A A =NewA ();2A.age = One;3A.name ="Jack";4b b =NewB ();5B.sex ="Mans";6 //B.money = n;7A.B =b;8 stringstr =Newtonsoft.Json.JsonConvert.SerializeObject (a);9 TenOutput Result: {" Age": One,"name":"Jack","B": {"Sex":"Mans"," Money":""}}

Examples of using deserializeobject:

1 stringJsonstr =@"{"Age": One,"Name": "Jack", "B": {"Sex": "Mans", "Money": ""}}";2 varA = newtonsoft.json.jsonconvert.deserializeobject<a>(JSONSTR);3 4Result: A.age = One;.......

OK, so, how do you use dynamic to parse a JSON string?

1 stringJsonstr ="{\ "data\": {\ "ssotoken\": \ "70abd3d8a6654ff189c482fc4842468c\", \ "account\": \ "admin\", \ "usertype\": \ " Platformadmin\ ", \" realname\ ": \" Super Administrator \ ", \" sex\ ": 0,\" sexname\ ": \" male \ ", \" email\ ": \" [Email protected]\ ", \" mobile\ ": \ "15120757948\", \ "createddt\": \ "2013-08-16 00:00:00\", \ "updateddt\": \ "2014-12-10 00:00:00\"},\ "issuccess\": true} "; 2 3 varuser = Logininfo.data;varLogininfo = jsonconvert.deserializeobject<dynamic>(JSONSTR);4 5 stringSsotoken =User.ssotoken;6 stringaccount = User.account;

This way, you can parse the JSON without creating a logininfo,user, and you won't get an error by adding a field there.

Tata Rabbit Two, share with you ...

Parsing a JSON string with the dynamic tag

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.