C # anonymous serialization and deserialization,
Preface
We currently advocate frontend and backend separation. After the separation, all services use interfaces to provide services to the front end. When we process custom queries, multiple tables will be queried, when processing multi-table queries, we are too lazy to create a view model, and too many projects are messy. Therefore, the dao layer uses the anonymous type to return json when processing custom queries.
Serialization and deserialization
List <object> oData = new List <object> (); oData. add (new {ID = 1, Name = "kd", Age = 31}); oData. add (new {ID = 2, Name = "kb", Age = 32}); string json = fastJSON. JSON. instance. toJSON (oData); Console. writeLine (json); oData = fastJSON. JSON. instance. toObject <List <dynamic> (json); // receives the result from List <object> deserialization with List <dynamic>, after deserialization, access the attribute foreach (dynamic o in oData) {Console. writeLine (o ["ID"] + "," + o ["Name"]);}