Json.NET - Documentation Creating JSON

來源:互聯網
上載者:User

標籤:des   style   blog   http   os   io   ar   cti   

Setting values and creating objects and arrays one at a time gives you total control but it is more verbose than other options.

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 way 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."),

10                new JProperty("item",

11                    new JArray(

12                        from p in posts

13                        orderby p.Title

14                        select new JObject(

15                            new JProperty("title", p.Title),

16                            new JProperty("description", p.Description),

17                            new JProperty("link", p.Link),

18                            new JProperty("category",

19                                new JArray(

20                                    from c in p.Categories

21                                    select new 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//   


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.