1. JSON serialization
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using System. web. script. serialization; namespace test2 {public partial class WebForm6: System. web. UI. page {public class Personnel {public int Id {get; set;} public string Name {get; set ;}} protected void Page_Load (object sender, EventArgs e) {Personnel personnel = new Personnel (); personnel. id = 1; personnel. name = ""; JavaScriptSerializer jsonSerializer = new JavaScriptSerializer (); // execute serialization lgx. text = jsonSerializer. serialize (personnel); // output result: {"Id": 1, "Name": ""}}}}
You can use the ScriptIgnore attribute to mark public or public fields that are not serialized.
public class Personnel { [ScriptIgnore] public int Id { get; set; } public string Name { get; set; } }
Output result: {"Name": ""}
2. JSON deserialization
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using system. web. script. serialization; namespace Test2 {public partial class webform6: system. web. UI. page {public class personnel {public int ID {Get; set;} public string name {Get; Set ;}} protected void page_load (Object sender, eventargs E) {personnel = new personnel (); personnel. id = 1; personnel. name = ""; javascriptserializer jsonserializer = new javascriptserializer (); // execute serialized string it = jsonserializer. serialize (Personnel); // output result: {"ID": 1, "name": ""} personnel _ personnel = jsonserializer. deserialize <personnel> (it); lgx. TEXT = "ID" + _ personnel. id. tostring () + "name" + _ personnel. name ;}}}