我想在 vb.net 中创建一个匿名类,如下所示:
var data = new {
total = totalPages,
page = page,
records = totalRecords,
rows = new[]{
new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
}
};
thx.
VB.NET 2008 没有new[]
构造,但 VB.NET 2010 可以。您无法直接在 VB.NET 2008 中创建匿名类型数组。技巧是声明一个如下所示的函数:
Function GetArray(Of T)(ByVal ParamArray values() As T) As T()
Return values
End Function
并让编译器为我们推断类型(因为它是匿名类型,所以我们无法指定名称)。然后像这样使用它:
Dim jsonData = New With { _
.total = totalPages, _
.page = page, _
.records = totalRecords, _
.rows = GetArray( _
New With {.id = 1, .cell = GetArray("1", "-7", "Is this a good question?")}, _
New With {.id = 2, .cell = GetArray("2", "15", "Is this a blatant ripoff?")}, _
New With {.id = 3, .cell = GetArray("3", "23", "Why is the sky blue?")}
) _
}
附言。这不叫 JSON。它称为匿名类型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)