javascript - 复杂的 C# 对象

标签 javascript c# .net asp.net-web-api

我习惯于制作如下所示的 JavaScript 对象,但用 C# 制作相同对象的最快方法是什么?它只是将为不会更改的帖子序列化的静态数据。在过去使用 C# 时,我会为所有内容创建类和列表,但在这种情况下,我只需要发送一些练习数据。任何帮助将不胜感激

            var data = {
                "dealer": {
                    "keyId": "vfase32sd",  
                     "name": "mark"
                },
                "seller": [
                    {
                        "email": "email@email.com",
                        "active":false
                    }
                ],
                "state": [
                    "TN",
                    "TX"
                ]};

最佳答案

参见 Object and Collection Initializers

Object initializer with Anonymous types

Although object initializers can be used in any context, they are especially useful in LINQ query expressions. Query expressions make frequent use of anonymous types, which can only be initialized by using an object initializer, as shown in the following declaration.

var pet = new { Age = 10, Name = "Fluffy" };

例如:

var data = new
{
    dealer = new
    {
        keyId = "vfase32sd",
        name = "mark"
    },
    seller = new[] {
        new {
            email= "email@email.com",
            active= false
        }
    },
    state = new[]{
        "TN",
        "TX"
    }
};

将 js 对象转换为 c# 对象的规则很简单:

  • "x": 将是 x=
  • {} 将是新的{}
  • [] 将是 new []{}
  • 值(value)观保持不变

关于javascript - 复杂的 C# 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32642021/

相关文章:

javascript - AmCharts 基于数据的动态列

c# - 从 ServiceStack 返回一个包含一些 JSON 对象的字符串

c# - 我应该为 hh :mm tt in SQL Server 保存什么数据类型

c# - ASP.NET MVC 与 Winforms MVC

.net - 使用 web.config 覆盖 machine.config

JavaScript 和动态创建对象

javascript - Django 脆皮表单 - 使用单选按钮隐藏/显示其他字段

javascript - 按属性对 JavaScript 对象数组进行排序时数字有什么问题

C# 在 asp.net 中禁用 GridView header

c# - FluentAssertion 无法比较可枚举的字符串