JSON file read/write

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.