我想获得一个对象数组,并根据键获取一个值。
像这样的javascript:
PageMethods.GetProducts(function(results) {
var productName = results[0].name;
});
我在后面的代码中尝试了这个,但我在结果中得到了一个数组数组:
VB
<WebMethod()>
Public Shared Function GetProducts() As ArrayList
Dim products As New ArrayList
Dim prAdptr As New DataSet1TableAdapters.ProductsTableAdapter
Dim prTbl As DataSet1.ProductsDataTable = prAdptr.GetData
Dim prRow As DataSet1.ProductsRow
For Each prRow In prTbl
Dim product As New Collection
product.Add(prRow.ProductID, "id")
product.Add(prRow.ProductName, "name")
products.Add(product)
Next
Return products
End Function
C#
[WebMethod()]
public static ArrayList GetProducts()
{
ArrayList products = new ArrayList();
DataSet1TableAdapters.ProductsTableAdapter prAdptr = new DataSet1TableAdapters.ProductsTableAdapter();
DataSet1.ProductsDataTable prTbl = prAdptr.GetData;
DataSet1.ProductsRow prRow = default(DataSet1.ProductsRow);
foreach ( prRow in prTbl) {
Collection product = new Collection();
product.Add(prRow.ProductID, "id");
product.Add(prRow.ProductName, "name");
products.Add(product);
}
return products;
}
因此我无法根据钥匙获得价值。 我知道我可以像这样引用值的位置:
results[0][0];
但这并不理想。
任何帮助都将不胜感激。
答案 0 :(得分:1)
您可以使用Anonymous Types
http://msdn.microsoft.com/en-us/library/bb397696.aspx
foreach ( prRow in prTbl) {
products.Add(new {
id=prRow.ProductID,
name=prRow.ProductName
});
}