public void Example() { #region Usage string json = @"[ { 'Title': 'Json.NET is awesome!', 'Author': { 'Name': 'James Newton-King', 'Twitter': '@JamesNK', 'Picture': '/jamesnk.png' }, 'Date': '2013-01-23T19:30:00', 'BodyHtml': '<h3>Title!</h3>\r\n<p>Content!</p>' } ]"; JArray blogPostArray = JArray.Parse(json); IList <BlogPost> blogPosts = blogPostArray.Select(p => new BlogPost { Title = (string)p["Title"], AuthorName = (string)p["Author"]["Name"], AuthorTwitter = (string)p["Author"]["Twitter"], PostedDate = (DateTime)p["Date"], Body = HttpUtility.HtmlDecode((string)p["BodyHtml"]) }).ToList(); Console.WriteLine(blogPosts[0].Body); // <h3>Title!</h3> // <p>Content!</p> #endregion StringAssert.AreEqual(@"<h3>Title!</h3> <p>Content!</p>", blogPosts[0].Body); }
public void Example() { #region Usage IList <BlogPost> blogPosts = new List <BlogPost> { new BlogPost { Title = "Json.NET is awesome!", AuthorName = "James Newton-King", AuthorTwitter = "JamesNK", PostedDate = new DateTime(2013, 1, 23, 19, 30, 0), Body = @"<h3>Title!</h3><p>Content!</p>" } }; JArray blogPostsArray = new JArray( blogPosts.Select(p => new JObject { { "Title", p.Title }, { "Author", new JObject { { "Name", p.AuthorName }, { "Twitter", p.AuthorTwitter } } }, { "Date", p.PostedDate }, { "BodyHtml", HttpUtility.HtmlEncode(p.Body) }, }) ); Console.WriteLine(blogPostsArray.ToString()); // [ // { // "Title": "Json.NET is awesome!", // "Author": { // "Name": "James Newton-King", // "Twitter": "JamesNK" // }, // "Date": "2013-01-23T19:30:00", // "BodyHtml": "<h3>Title!</h3><p>Content!</p>" // } // ] #endregion StringAssert.AreEqual(@"[ { ""Title"": ""Json.NET is awesome!"", ""Author"": { ""Name"": ""James Newton-King"", ""Twitter"": ""JamesNK"" }, ""Date"": ""2013-01-23T19:30:00"", ""BodyHtml"": ""<h3>Title!</h3><p>Content!</p>"" } ]", blogPostsArray.ToString()); }