C # Serializes an object into a JSON string

Source: Internet
Author: User

  1. Public string getjsonstring ()
  2. {
  3. list<product> products = new list<product> () {
  4. New Product () {name="Apple", price=5.5},
  5. New Product () {name="orange", price=2.5},
  6. New Product () {name="dried persimmon", price=16.00}
  7. };
  8. ProductList productlist = new ProductList ();
  9. ProductList. GetProducts = Products;
  10. return new JavaScriptSerializer ().  Serialize (productlist));
  11. }
  12. Public class Product
  13. {
  14. public string Name { get;  set;}
  15. public Double price { get;  set;}
  16. }
  17. Public class ProductList
  18. {
  19. Public list<product> getproducts { get;  set;}
  20. }

The main thing here is to use JavaScriptSerializer to implement serialization, so that we can convert the object into a JSON-formatted string, resulting in the following results:

    1. {"getproducts": [{"name":"apple","price": 5.5},{"name":"Orange","price": 2.5},{"name": "Persimmon","price": 16}]}

How do I convert a JSON string into an object?

In actual development, it is often possible to encounter using JS to pass a JSON-formatted string into the background, if you can automatically convert the string to the desired object, it is more convenient to traverse or other operations. How is that specific?

  1. Public static list<t> jsonstringtolist<t> (this string jsonstr)
  2. {
  3. JavaScriptSerializer serializer = new JavaScriptSerializer ();
  4. List<t> Objs = serializer.deserialize<list<t>> (JSONSTR);
  5. return OBJS;
  6. }
  7. Public static T deserialize<t> (string json)
  8. {
  9. T obj = activator.createinstance<t> ();
  10. using (MemoryStream ms = New MemoryStream (Encoding.UTF8.GetBytes (JSON)))
  11. {
  12. DataContractJsonSerializer serializer = new DataContractJsonSerializer (obj.  GetType ());
  13. return (T) serializer.  ReadObject (MS);
  14. }
  15. }
  16. String jsonstr = "[{Name: ' Apple ', Price:5.5},{name: ' Orange ', price:2.5},{name: ' Persimmon ', price:16}]";
  17. list<product> products = new list<product> ();
  18. Products = jsonstringtolist<product> (JSONSTR);
  19. foreach (var item in products )
  20. {
  21. Response.Write (item. Name + ":" + Item.  Price + "<br/>");
  22. }
  23. Public class Product
  24. {
  25. public string Name { get;  set;}
  26. public Double price { get;  set;}
  27. }

In the above example, it is convenient to convert the JSON string into a list object, the operation of the time is more convenient ~

C # Serializes an object into a JSON string

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.