我想获取一个对象数组并根据键获取一个值。
像这样的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];
但这并不理想。
任何帮助,将不胜感激。