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