namespace web{public partial class TestJson:System.Web.UI.Page {protected void Page_Load (object sender, E Ventargs e) {Configfilemodel cfm = new Configfilemodel (); Entity Model class CFM. CreateDate = "2012-02-23"; cfm. FileName = "Test.txt"; You can manipulate the collection Hashtable HT = new Hashtable (); Ht. ADD ("Hs_name", "Alex"); Ht. ADD ("Hs_pwd", "GGG"); Serialization of String js1 = Jsonconvert.serializeobject (CFM); String js2 = Jsonconvert.serializeobject (HT); Response.Write (JS1); Response.Write ("<br/>"); Response.Write (JS2); "{\" filename\ ": \" test.txt\ ", \" createdate\ ": \" 2012-02-23\ "}"//"{\" hs_pwd\ ": \" ggg\ ", \" hs_name\ ": \" alex\ "}" Deserialization of Configfilemodel DEBC1 = jsonconvert.deserializeobject<configfilemodel> (JS1); Configfilemodel DEBC2 = jsonconvert.deserializeobject<configfilemodel> (JS2); Locate the server physical path//string Serverapppath = Request.PhysicalApplicationPath.ToString (); String serverapppath = @ "D:\"; form the configuration file path string Con_file_path [email protected] "" + Serverapppath + @ "Config.json"; if (! File.exists (Con_file_path)) {file.create (Con_file_path); }//write the model data to the file using (StreamWriter SW = new StreamWriter (Con_file_path)) {try {Jsonserializer serializer = new Jsonserializer (); Serializer. Converters.add (New Javascriptdatetimeconverter ()); Serializer. nullvaluehandling = Nullvaluehandling.ignore; Build Json.NET Write stream jsonwriter writer = new JsonTextWriter (SW); The model data is serialized and written to the Json.NET Jsonwriter stream serializer. Serialize (writer, cfm); Ser. Serialize (writer, ht); Writer. Close (); Sw. Close (); }} catch (Exception ex) {ex. Message.tostring (); }//Read JSON file using (StreamReader sr = new StreamReader (Con_file_path)) {try {Jsonserializer serializer = new Jsonserializer (); Serializer. Converters.add (New Javascriptdatetimeconverter ()); Serializer. nullvaluehandling = Nullvaluehandling.ignore; Build Json.NET Read stream Jsonreader reader = new JsonTextReader (SR); The reader stream of the read-out Json.NET is deserialized and loaded into the model CFM = serializer. Deserialize<configfilemodel> (reader); Response.Write ("<br/>"); Response.Write (CFM. Filename+ "," +cfm. CreateDate); } catch (Exception ex) {ex. Message.tostring (); }}}} public class Configfilemodel {public Configfilemodel () {} String _filename; public string FileName {get {return _filename;} set {_filename = value;} } string _createdate; public string CreateDate {get {return _createdate;} set {_createdate = value;} } }}
JSON file read/write