Setting values and creating objects and arrays one at a time gives all control but it's more verbose than other opt Ions.
Creating JSON Copy imagecopy
1JArray array = new Jarray ();
2JValue Text = new Jvalue ("Manual text");
3JValue date = new Jvalue (New DateTime (2000, 5, 23));
4
5array. ADD (text);
6array. ADD (date);
7
8string JSON = array. ToString ();
9//[
10//"Manual text",
11//"2000-05-23t00:00:00"
12//]
Creating JSON with LINQ
Declaratively creating JSON objects using LINQ is a fast-to-create JSON from collections of values.
Creating JSON declaratively Copy imagecopy
1list<post> posts = getposts ();
2
3JObject RSS =
4 New Jobject (
5 new Jproperty ("channel",
6 New Jobject (
7 new Jproperty ("title", "James newton-king"),
8 new Jproperty ("link", "http://james.newtonking.com"),
9 New Jproperty ("description", "James newton-king ' s blog."),
Ten new Jproperty ("Item",
New Jarray (
From P in posts
P.title
-Select New Jobject (
New Jproperty ("title", P.title),
New Jproperty ("description", p.description),
New Jproperty ("link", P.link),
New Jproperty ("category",
New Jarray (
C in P.categories
(Jvalue (c)))));
22
23console.writeline (RSS. ToString ());
24
25//{
26//"Channel": {
27//"title": "James newton-king",
28//"link": "Http://james.newtonking.com",
29//"description": "James newton-king's blog.",
30//"Item": [
31//{
32//"title": "Json.NET 1.3 + NEW license + now on CodePlex",
33//"description": "Annoucing the release of Json.NET 1.3, the MIT license and being available on CodePlex",
34//"link": "Http://james.newtonking.com/projects/json-net.aspx",
35//