如何使用for循环从动态对象获取行数?

时间:2016-10-20 02:28:52

标签: c# web-services xamarin visual-studio-2015 xamarin.forms

您好我是编程的新手,目前我正在尝试获取存储在动态对象中的arraylist的行数,我使用Web服务检索值,所以我希望有人可以帮助我目前的问题。

以下是我的代码:

.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LCafeMealOrdering.Models;
using Xamarin.Forms;
using System.Net.Http;
using Newtonsoft.Json;
using System.Net.Http.Headers;

namespace LCafeMealOrdering
{
    public partial class PageHome : ContentPage
    {
        static MenuItemModel miObj = new MenuItemModel();
        static List<MenuItemModel> mioList = new List<MenuItemModel>();
        public PageHome()
        {
            InitializeComponent();
            var abc = getMenuList();

            HomepageListView.ItemsSource = mioList;

        }

        private void listSearchBar_OnSearchButtonPressed(object sender, EventArgs e)
        {
            string keyword = listSearchBar.Text;
        }

        private void HomepageListView_OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            if (e.SelectedItem != null)
            {
                var selection = e.SelectedItem as MenuItemModel;
                DisplayAlert("You have selected ", selection.item_Name, "OK");
                ((ListView)sender).SelectedItem = null;
                string iImage = selection.item_Image;
                string iName = selection.item_Name;
                double iHotPrice = selection.item_HotPrice;
                double iColdPrice = selection.item_ColdPrice;
                string iDesc = selection.item_Description;

                Navigation.PushAsync(new PageMenuAddItem(iImage, iName, iHotPrice, iColdPrice, iDesc));
            }
        }
        //End HomepageListView_OnItemSelected

        public async Task<List<MenuItemModel>> getMenuList()
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://172.20.129.44/");

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = new HttpResponseMessage();
            response = client.GetAsync("WebServices/menu.svc/GetMenuJSON").Result;

            if (response.IsSuccessStatusCode)
            {
                string jsonString = await response.Content.ReadAsStringAsync();
                dynamic dynamicObject = JsonConvert.DeserializeObject(jsonString);

                for (int i = 0; i < dynamicObject.Length; i++)
                {
                    int itemId_ = dynamicObject.d[i]["itemID"];
                    string itemName_ = dynamicObject.d[i]["itemName"].ToString();
                    string itemCategory_ = dynamicObject.d[i]["itemCategory"].ToString();
                    string itemSubCategory_ = dynamicObject.d[i]["itemCategory"].ToString();
                    string itemDescription_ = dynamicObject.d[i]["itemDesc"].ToString();
                    string itemImage_ = dynamicObject.d[i]["itemImg"].ToString();
                    int itemHotQuantity_ = dynamicObject.d[i]["itemQty"].ToString();
                    int itemColdQuantity_ = dynamicObject.d[i]["itemQty"].ToString();
                    double itemHotPrice_ = dynamicObject.d[i]["itemPrice"].ToString();
                    double itemColdPrice_ = dynamicObject.d[i]["itemPrice"].ToString();
                    mioList.Add(new MenuItemModel(itemId_, itemName_, itemCategory_, itemSubCategory_, itemDescription_, itemImage_, itemHotQuantity_, itemColdQuantity_, itemHotPrice_, itemColdPrice_));
                }


            }
            else
            {
                //Debug.WriteLine("It entered else not if");
            }
            return mioList;

        }

        //End of Partial Class
    }
}

网络服务

http://172.20.129.44/WebServices/menu.svc/GetMenuJSON

网络服务价值

{"d":[{"__type":"CafeMenu:#website.Model","itemCategory":"Cake","itemDesc":"Nice","itemID":1,"itemImg":imagepath, "itemName":"Cheesecake, American","itemPrice":3.00,"itemQty":30,"itemStatus":""}, {"__type":"CafeMenu:#website.Model","itemCategory":"Cake","itemDesc":"Berry","itemID":2,"itemImg":imagePath, "itemName":"Chocolate","itemPrice":1.80,"itemQty":10,"itemStatus":""}]}

2 个答案:

答案 0 :(得分:1)

我认为你的意思是dynamicObject.d.Count,因为d是对象中的集合

for (int i = 0; i < dynamicObject.d.Count; i++)
{
    int itemId_ = dynamicObject.d[i].itemID;
    string itemName_ = dynamicObject.d[i].itemName;
    string itemCategory_ = dynamicObject.d[i].itemCategory;
    string itemSubCategory_ = dynamicObject.d[i].itemCategory;
    string itemDescription_ = dynamicObject.d[i].itemDesc;
    string itemImage_ = dynamicObject.d[i].itemImg;
    int itemHotQuantity_ = dynamicObject.d[i].itemQty;
    int itemColdQuantity_ = dynamicObject.d[i].itemQty;
    double itemHotPrice_ = dynamicObject.d[i].itemPrice;
    double itemColdPrice_ = dynamicObject.d[i].itemPrice;
    mioList.Add(new MenuItemModel(itemId_, itemName_, itemCategory_, itemSubCategory_, itemDescription_, itemImage_, itemHotQuantity_, itemColdQuantity_, itemHotPrice_, itemColdPrice_));
}

此外,由于对象作为动态对象被访问,因此可以根据分配的类型推断出类型。

foreach也可以与数组一起使用

foreach (dynamic item in dynamicObject.d)
{
    int itemId_ = item.itemID;
    string itemName_ = item.itemName;
    string itemCategory_ = item.itemCategory;
    string itemSubCategory_ = item.itemCategory;
    string itemDescription_ = item.itemDesc;
    string itemImage_ = item.itemImg;
    int itemHotQuantity_ = item.itemQty;
    int itemColdQuantity_ = item.itemQty;
    double itemHotPrice_ = item.itemPrice;
    double itemColdPrice_ = item.itemPrice;
    mioList.Add(new MenuItemModel(itemId_, itemName_, itemCategory_, itemSubCategory_, itemDescription_, itemImage_, itemHotQuantity_, itemColdQuantity_, itemHotPrice_, itemColdPrice_));
}

答案 1 :(得分:0)

您正在尝试访问整个结果,您需要访问“d”

 for (int i = 0; i < dynamicObject.d.Length; i++)
{
}