Json.NET reading and writing JSON files

Source: Internet
Author: User

  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Web;
  5. Using System.Web.UI;
  6. Using System.Web.UI.WebControls;
  7. Using System.IO;
  8. Using System.Text;
  9. Using Newtonsoft.json;
  10. Using Com.zjpx.model;
  11. Using System.Collections;
  12. Using Newtonsoft.Json.Converters;
  13. Namespace Web
  14. {
  15. Public partial class TestJson:System.Web.UI.Page
  16. {
  17. protected void Page_Load (object sender, EventArgs e)
  18. {
  19. Configfilemodel cfm = new Configfilemodel (); //Entity model class
  20. cfm.  CreateDate = "2012-02-23";
  21. cfm.  FileName = "Test.txt";
  22. //Can manipulate the collection
  23. Hashtable ht = new Hashtable ();
  24. Ht.  ADD ("Hs_name", "Alex");
  25. Ht.  ADD ("Hs_pwd", "GGG");
  26. //Serialization
  27. string js1 = Jsonconvert.serializeobject (CFM);
  28. string js2 = Jsonconvert.serializeobject (HT);
  29. Response.Write (JS1);
  30. Response.Write ("<br/>");
  31. Response.Write (JS2);
  32. //"{\" filename\ ": \" test.txt\ ", \" createdate\ ": \" 2012-02-23\ "}"
  33. //"{\" hs_pwd\ ": \" ggg\ ", \" hs_name\ ": \" alex\ "}"
  34. //Deserialization
  35. Configfilemodel DEBC1 = jsonconvert.deserializeobject<configfilemodel> (JS1);
  36. Configfilemodel DEBC2 = jsonconvert.deserializeobject<configfilemodel> (JS2);
  37. //Find the server physical path
  38. //string Serverapppath = Request.PhysicalApplicationPath.ToString ();
  39. string serverapppath = @"D:\";
  40. //Make configuration file path
  41. string Con_file_path [email protected] "" + Serverapppath + @"Config.json";
  42. if (! File.exists (Con_file_path))
  43. {
  44. File.create (Con_file_path);
  45. }
  46. //write the model data to a file
  47. using (StreamWriter SW = new StreamWriter (Con_file_path))
  48. {
  49. Try
  50. {
  51. Jsonserializer serializer = new Jsonserializer ();
  52. Serializer.  Converters.add (new Javascriptdatetimeconverter ());
  53. Serializer. nullvaluehandling = Nullvaluehandling.ignore;
  54. //Build Json.NET Write stream
  55. Jsonwriter writer = new JsonTextWriter (SW);
  56. //Serialize the model data and write to the json.net Jsonwriter stream
  57. Serializer. Serialize (writer, cfm);
  58. //ser.  Serialize (writer, ht);
  59. Writer. Close ();
  60. Sw. Close ();
  61. }
  62. }
  63. catch (Exception ex)
  64. {
  65. Ex. Message.tostring ();
  66. }
  67. //Read JSON file
  68. using (StreamReader sr = New StreamReader (Con_file_path))
  69. {
  70. Try
  71. {
  72. Jsonserializer serializer = new Jsonserializer ();
  73. Serializer.  Converters.add (new Javascriptdatetimeconverter ());
  74. Serializer. nullvaluehandling = Nullvaluehandling.ignore;
  75. //Build json.net Read stream
  76. Jsonreader reader = new JsonTextReader (SR);
  77. //Json.NET the reader stream of the read-out to be loaded into the model
  78. CFM = Serializer. Deserialize<configfilemodel> (reader);
  79. Response.Write ("<br/>");
  80. Response.Write (CFM. filename+"," +cfm.  CreateDate);
  81. }
  82. catch (Exception ex)
  83. {
  84. Ex. Message.tostring ();
  85. }
  86. }
  87. }
  88. }
  89. public class Configfilemodel
  90. {
  91. Public Configfilemodel () {}
  92. string _filename;
  93. public string FileName
  94. {
  95. get { return _filename;}
  96. set {_filename = value;}
  97. }
  98. string _createdate;
  99. public string CreateDate
  100. {
  101. get { return _createdate;}
  102. set {_createdate = value;}
  103. }
  104. }
  105. }

Json.NET reading and writing JSON files

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.