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

  • 相关阅读:
    POJ 1654 Area 多边形面积 G++会WA
    POJ 3348 Cows 求凸包面积
    POJ 1279 Art Gallery 半平面交 多边形的核
    hdu 1556 Color the ball 线段树 区间更新
    POJ 1474 Video Surveillance 半平面交
    POJ 3130 How I Mathematician Wonder What You Are! 半平面交
    在React项目中,如何优雅的优化长列表
    react高亮显示关键词
    通过a标签同源和跨域下载服务器文件(基于blob)
    前端性能优化之回流和重绘
  • 原文地址:https://www.cnblogs.com/Fred1987/p/13275746.html
Copyright © 2020-2023  润新知