Json.net-documentation Creating JSON

Source: Internet
Author: User
Tags imagecopy

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//


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.