Write and read of JSON files
JSON. Net API.
Http://james.newtonking.com/projects/json-net.aspx
Http://www.codeplex.com/json/
The following is the Demo code.
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using system. io; using system. text; using newtonsoft. JSON; using COM. zjpx. model; using system. collections; using newtonsoft. JSON. converters; namespace web {public partial class testjson: system. web. UI. page {protected void page_load (Object sender, eventargs e) {configfile Model CFM = new configfilemodel (); // entity model class CFM. createdate = "2012-02-23"; CFM. filename = "test.txt"; // you can operate the hashtable ht = new hashtable (); ht. add ("hs_name", "Alex"); ht. add ("hs_pwd", "ggg"); // serialize string JS1 = jsonconvert. serializeobject (CFM); string JS2 = jsonconvert. serializeobject (HT); response. write (JS1); response. write ("<br/>"); response. write (JS2); // "{\" FILENAME \ ": \" test.txt \ ", \" createda Te \ ": \" 2012-02-23 \ "}" // "{\" hs_pwd \ ": \" ggg \ ", \" hs_name \": \ "Alex \"} "// deserialization 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: \"; // the configuration file path string con_file_path = @ "" + serverapppath + @ "con Fig. 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; // construct the json.net write stream jsonwriter writer = new jsontextwriter (SW); // serialize model data and write it to serializer in jsonwriter stream of json.net. serialize (writer, CFM); // Ser. serialize (writer, HT); writer. close (); Sw. close () ;}} catch (exception ex) {ex. message. tostring () ;}// read the JSON file using (streamreader sr = new streamreader (con_file_path) {try {jsonserializer serializer = new jsonserializer (); serializer. converters. add (New javascriptdatetimeconverter (); serializer. nullvaluehandling = nullvaluehandling. ignore; // construct the json.net read stream jsonreader reader = new jsontextreader (SR); // deserialize the read json.net reader stream, and load it to 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 ;}}}}