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"
        }
      }
    }
  }
}

C# anonymous types and serialization_第1张图片

 

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

C# anonymous types and serialization_第2张图片

 

你可能感兴趣的:(C# anonymous types and serialization)