Implementation of A. NET generic JSON parsing/construction class

Source: Internet
Author: User

In. NET Framework 3.5 provides a JSON Object serialization tool, but it is strongly typed. You must first define a type according to the JSON object format and add the type to the JSON serialization feature. This article will try to provide a highly flexible JSON generic type (JsonObject) for JSON parsing and serialization.

Assume that the content of the JSON object is as follows:

  1. {
  2.     orders: {
  3.         date: 21:31:59,
  4.         name: Xfrog,
  5.         books: [{
  6. Name: C # core network programming,
  7.             publish: 2010-3-24
  8.         }, {
  9. Name: C # entry-level Classic Chinese edition,
  10.             publish: 2009-10-16
  11.         }]
  12.     },
  13.     blog: http://www.cnblogs.com/xfrog
  14. }

 

You can use the following methods to build a JsonObject:
Method 1:

  1. // Use the standard Constructor
  2. JsonObject json = new JsonObject();
  3. json["orders"] = new JsonProperty(new JsonObject());
  4. json["blog"] = new JsonProperty("http://www.cnblogs.com/xfrog");
  5.  
  6. JsonObject config = json.Properties<JsonObject>("orders");
  7. json["orders"]["date"] = new JsonProperty(DateTime.Now.ToLongTimeString());
  8. json["orders"]["name"] = new JsonProperty("Xfrog");
  9. json["orders"]["books"] = new JsonProperty();
  10.  
  11. JsonProperty book = json["orders"]["books"].Add(new JsonObject());
  12. Book ["name"] = new JsonProperty ("C # core network programming ");
  13. book["publish"] = new JsonProperty("2010-3-24");
  14.  
  15. book = json["orders"]["books"].Add(new JsonObject());
  16. Book ["name"] = new JsonProperty ("C # ");
  17. book["publish"] = new JsonProperty("2009-10-16");

 


Method 2:

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.