Json. NET reads and writes Json files, and json.net writes json files.

Source: Internet
Author: User

Json. NET reads and writes Json files, and json.net writes json files.

  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. // Sets that can be operated
  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 physical path of the server
  38. // String serverAppPath = Request. PhysicalApplicationPath. ToString ();
  39. String serverAppPath = @ "d :\";
  40. // Configure the configuration file path
  41. String con_file_path = @ "" + serverAppPath + @ "config. json ";
  42. If (! File. Exists (con_file_path ))
  43. {
  44. File. Create (con_file_path );
  45. }
  46. // Write 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. // Construct the Json.net write stream
  55. JsonWriter writer = new JsonTextWriter (sw );
  56. // Serialize the model data and write it into the JsonWriter stream of Json.net.
  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 the 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. // Construct the Json.net read stream
  76. JsonReader reader = new JsonTextReader (sr );
  77. // Deserialize the read-out Json.net reader stream and load it to 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. }

Related Article

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.