ASP. NET uses JSON (. net3.5, C #)

Source: Internet
Author: User

. NET Framework 3.5 has built-in support for JSON, which can be directly used.
I. Preparation
First, reference: system. servicemodel. Web. dll, system. runtime. serialization. dll.
Then in using

 
  1. using System.Runtime.Serialization;  
  2. using System.Runtime.Serialization.Json; 

II. Compile the classes to be serialized

 
  1. using System.Runtime.Serialization;  
  2. namespace Dockty.Data  
  3. {  
  4.     [DataContract(Namespace = "http://www.dockty.com")]  
  5.     public class ArtTags  
  6.     {  
  7.         public ArtTags() { }  
  8.         [DataMember(Order = 0)]  
  9.         public int ID { get; set; }  
  10.         [DataMember(Order = 1)]  
  11.         public int TagID { get; set; }  
  12.         [DataMember(Order = 2)]  
  13.         public int ArtID { get; set; }          
  14.     }  
  15. } 

Define the object and sequence to be serialized.

3. JSON help class

 
  1. Using system. IO;
  2. Using system. runtime. serialization. JSON;
  3. Using system. text;
  4.  
  5. Namespace dockty. Data
  6. {
  7. Public static class jsonhelper <t>
  8. {
  9. Static jsonhelper ()
  10. {}
  11.  
  12. /// <Summary>
  13. /// Serialized as a JSON object
  14. /// </Summary>
  15. /// <Param name = "OBJ"> </param>
  16. /// <Returns> </returns>
  17. Public static string writeobject (t obj)
  18. {
  19. VaR serializer = new datacontractjsonserializer (typeof (t ));
  20. VaR mstream = new memorystream ();
  21. Serializer. writeobject (mstream, OBJ );
  22. Byte [] bytes = new byte [mstream. Length];
  23. Mstream. Position = 0;
  24. Mstream. Read (bytes, 0, (INT) mstream. Length );
  25. Return encoding. utf8.getstring (bytes );
  26. }
  27.  
  28. /// <Summary>
  29. /// Deserialization of JSON objects
  30. /// </Summary>
  31. /// <Param name = "data"> </param>
  32. /// <Returns> </returns>
  33. Public static t readobject (string data)
  34. {
  35. VaR mstream = new memorystream (encoding. utf8.getbytes (data ));
  36. VaR serializer = new datacontractjsonserializer (typeof (t ));
  37. Return (t) serializer. readobject (mstream );
  38. }
  39. }
  40. }

4. Serialization and deserialization

 
  1. Arttags tags = new arttags () {artid = 1, id = 1, tagid = 1 };
  2. // Serialization
  3. String strtags = jsonhelper <arttags>. writeobject (TAGS );
  4. // Deserialization
  5. Arttags newtags = jsonhelper <arttags>. readobject (strtags );
This article was edited by dockty at 10:55:10:

. Net 3.5, ASP. NET, JSON, C #

Reference announcement: Notice

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.