티스토리 뷰

목차

    반응형

    [C# JSON] JSON 생성 및 사용, 예제 소스


    C# JSON 생성


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    JArray array = new JArray();
    JValue text = new JValue("Manual text");
    JValue date = new JValue(new DateTime(2000523));
     
    array.Add(text);
    array.Add(date);
     
    string json = array.ToString();
    // [
    //   "Manual text",
    //   "2000-05-23T00:00:00"
    // ]
    cs


    C# JSON[[C# JSON] JSON 생성 및 사용, 예제 소스]


    C# LINQ, JSON 생성


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    List<Post> posts = GetPosts();
     
    JObject rss =
        new JObject(
            new JProperty("channel",
                new JObject(
                    new JProperty("title""James Newton-King"),
                    new JProperty("link""http://james.newtonking.com"),
                    new JProperty("description""James Newton-King's blog."),
                    new JProperty("item",
                        new JArray(
                            from p in posts
                            orderby 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(
                                        from c in p.Categories
                                        select new JValue(c)))))))));
     
    Console.WriteLine(rss.ToString());
     
    //{
    //  "channel": {
    //    "title": "James Newton-King",
    //    "link": "http://james.newtonking.com",
    //    "description": "James Newton-King\'s blog.",
    //    "item": [
    //      {
    //        "title": "Json.NET 1.3 + New license + Now on CodePlex",
    //        "description": "Annoucing the release of Json.NET 1.3, the MIT license and being available on CodePlex",
    //        "link": "http://james.newtonking.com/projects/json-net.aspx",
    //        "category": [
    //          "Json.NET",
    //          "CodePlex"
    //        ]
    //      },
    //      {
    //        "title": "LINQ to JSON beta",
    //        "description": "Annoucing LINQ to JSON",
    //        "link": "http://james.newtonking.com/projects/json-net.aspx",
    //        "category": [
    //          "Json.NET",
    //          "LINQ"
    //        ]
    //      }
    //    ]
    //  }
    //}
    cs


    C# Linq[[C# JSON] JSON 생성 및 사용, 예제 소스]


    객체 내부에서 C# JSON 생성


    FromObject 함수를 이용해 생성하는 방법으로 C# Linq와 직렬화가 가능합니다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    1JObject o = JObject.FromObject(new
     
    channel = new
    {
        title = "James Newton-King",
        link = "http://james.newtonking.com",
        description = "James Newton-King's blog.",
        item =
            from p in posts
            orderby p.Title
            select new
            {
                title = p.Title,
                description = p.Description,
                link = p.Link,
                category = p.Categories
            }
    });
    cs


    [C# JSON] JSON 생성 및 사용, 예제 소스

    반응형