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:
-
{
-
orders: {
-
date: 21:31:59,
-
name: Xfrog,
-
books: [{
-
Name: C # core network programming,
-
publish: 2010-3-24
-
}, {
-
Name: C # entry-level Classic Chinese edition,
-
publish: 2009-10-16
-
}]
-
},
-
blog: http://www.cnblogs.com/xfrog
-
}
You can use the following methods to build a JsonObject:
Method 1:
-
// Use the standard Constructor
-
JsonObject json = new JsonObject();
-
json["orders"] = new JsonProperty(new JsonObject());
-
json["blog"] = new JsonProperty("http://www.cnblogs.com/xfrog");
-
-
JsonObject config = json.Properties<JsonObject>("orders");
-
json["orders"]["date"] = new JsonProperty(DateTime.Now.ToLongTimeString());
-
json["orders"]["name"] = new JsonProperty("Xfrog");
-
json["orders"]["books"] = new JsonProperty();
-
-
JsonProperty book = json["orders"]["books"].Add(new JsonObject());
-
Book ["name"] = new JsonProperty ("C # core network programming ");
-
book["publish"] = new JsonProperty("2010-3-24");
-
-
book = json["orders"]["books"].Add(new JsonObject());
-
Book ["name"] = new JsonProperty ("C # ");
-
book["publish"] = new JsonProperty("2009-10-16");
Method 2: