我习惯于制作如下所示的 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/