GCK 1617 GCK 1617 - 1 month ago 6
C# Question

How to get the number of rows from a dynamic object using a for loop?

Hi I am new to programming, currently I am trying to get the number of rows of my arraylist which is stored in a dynamic object which i retrieve the values using the web services so i was hoping that anyone could help me with my current problem.

Here are my codes:

.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
}
}


Web Service

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


Web Service Value

{"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":""}]}

Answer

I think you meant dynamicObject.d.Count as d is the collection in the object

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_));
}

Also, as the object is being accessed as a dynamic object the type can be inferred from the type it is being assigned to.

foreach could also be used with the array

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_));
}
Comments