zoukankan      html  css  js  c++  java
  • C# anonymous types and serialization

    static void AnonymousSerializeDemo()
            {
                var data = new
                {
                    firstPart = new
                    {
                        item1 = "item1",
                        item2 = "item2",
                        item3 = 10
                    },
                    secondPart = new
                    {
                        pp1 = new
                        {
                            ii1 = "ii1",
                            ii2 = "ii2",
                            ii3 = 20
                        },
                        pp2 = new
                        {
                            ii2 = "ii2",
                            ii3 = "ii3",
                            ii5 = new
                            {
                                iii1 = "iii1",
                                iii2 = "iii2",
                                Student = new
                                {
                                    Id = 1,
                                    Name = "Fred1"
                                }
                            },
                        },
                    }
                };
    
                string jsonValue = JsonConvert.SerializeObject(data, Formatting.Indented);
                System.Diagnostics.Debug.WriteLine(jsonValue);
                Console.WriteLine(jsonValue);
            }

    Result:

    {
      "firstPart": {
        "item1": "item1",
        "item2": "item2",
        "item3": 10
      },
      "secondPart": {
        "pp1": {
          "ii1": "ii1",
          "ii2": "ii2",
          "ii3": 20
        },
        "pp2": {
          "ii2": "ii2",
          "ii3": "ii3",
          "ii5": {
            "iii1": "iii1",
            "iii2": "iii2",
            "Student": {
              "Id": 1,
              "Name": "Fred1"
            }
          }
        }
      }
    }

      static void AnonymousSerializeDemo()
            {
                var data = new
                {
                    Country=new
                    {
                        Name="USA",
                        Code="001",
                    },
                    State=new
                    {
                        Name = "NY",
                        Id = "001",
                        Companies = new
                        {
                            MS = new 
                            { Name = "MS", 
                                Id = 1, 
                                MarketCap = 1,
                                Grounp=new
                                {
                                    Name="Azure",
                                    Profit=2,
                                    Amount=10000,
                                    Founder="Bill Gates",
                                    DB=new
                                    {
                                      Name= "Oracle,MSSQL,MYSQL,PostSQL"
                                    },
                                    Language=new
                                    {
                                      C1= new
                                       {
                                           Name = "C#",
                                           Id = 1,
                                           traits=new
                                           {
                                               CLR=new
                                               {
                                                   Name="CLR",
                                                   Purpose="GC"
                                               },
                                               Delegate=new
                                               {
                                                   Name="Del",
                                                   p="Function pointer"
                                               },
                                               Relection=new
                                               {
                                                   Name="Reflection",
                                                   p="Assembly,Metadata"
                                               },
                                               MultiThreads=new
                                               {
                                                   lockC=new
                                                   {
                                                       Name="Lock",
                                                       p="lock(obj)"
                                                   },
                                                   Monitorc=new
                                                   {
                                                       Name="Monitor",
                                                       p="Monitor.Enter,Monitor.Exit"
                                                   },
                                                   Mutexc=new
                                                   {
                                                       Name="Mutex",
                                                       p="Exclude"
                                                   },
    
                                               }
                                           }
                                       },
                                      c2= new
                                       {
                                          Name="VC",
                                          Id=2
                                       }
                                    }, 
                                }
                            },
                            APPL = new { Name = "APPL", Id = 2, MarketCap = 2 },
                            Amazon = new { Name = "AWS", Id = 3, MarketCap = 3 }
                        }
                    }
                };
    
                string jsonValue = JsonConvert.SerializeObject(data, Formatting.Indented);
                System.Diagnostics.Debug.WriteLine(jsonValue);
                Console.WriteLine(jsonValue);
            }
    {
      "Country": {
        "Name": "USA",
        "Code": "001"
      },
      "State": {
        "Name": "NY",
        "Id": "001",
        "Companies": {
          "MS": {
            "Name": "MS",
            "Id": 1,
            "MarketCap": 1,
            "Grounp": {
              "Name": "Azure",
              "Profit": 2,
              "Amount": 10000,
              "Founder": "Bill Gates",
              "DB": {
                "Name": "Oracle,MSSQL,MYSQL,PostSQL"
              },
              "Language": {
                "C1": {
                  "Name": "C#",
                  "Id": 1,
                  "traits": {
                    "CLR": {
                      "Name": "CLR",
                      "Purpose": "GC"
                    },
                    "Delegate": {
                      "Name": "Del",
                      "p": "Function pointer"
                    },
                    "Relection": {
                      "Name": "Reflection",
                      "p": "Assembly,Metadata"
                    },
                    "MultiThreads": {
                      "lockC": {
                        "Name": "Lock",
                        "p": "lock(obj)"
                      },
                      "Monitorc": {
                        "Name": "Monitor",
                        "p": "Monitor.Enter,Monitor.Exit"
                      },
                      "Mutexc": {
                        "Name": "Mutex",
                        "p": "Exclude"
                      }
                    }
                  }
                },
                "c2": {
                  "Name": "VC",
                  "Id": 2
                }
              }
            }
          },
          "APPL": {
            "Name": "APPL",
            "Id": 2,
            "MarketCap": 2
          },
          "Amazon": {
            "Name": "AWS",
            "Id": 3,
            "MarketCap": 3
          }
        }
      }
    }

  • 相关阅读:
    QWrap简介之:EventW Event包装
    QWrap简介之:core_retouch 渲染原生类
    QWrap简介之:youa_retouch 项目个性
    QWrap简介之:Apps 应用 收获果实
    QWrap简介之:Wrap模式
    QWrap简介之:dom_retouch NodeW 勇士装甲
    Activity之间的数据传递
    OpenGL ES Tutorial for Android
    从零开始学习OpenGL ES之一 – 基本概念
    java自定义注解
  • 原文地址:https://www.cnblogs.com/Fred1987/p/13275746.html
Copyright © 2011-2022 走看看