JeroenM
JeroenM

Reputation: 837

Json get item from array in an array C# Xamarin

I have the following class for my Json data I receive:

[JsonObject(MemberSerialization.OptIn)]
public class OrderInfoResult
{
    [JsonProperty("ok")]
    public Boolean OK { get; set; }
    [JsonProperty("menu")]
    public List<Menu> Menu { get; set; }
}
public class Menu
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("icon-url")]
    public string IconUrl { get; set; }
    [JsonProperty("items")]
    public List<Menu> Items { get; set; }
}
public class Items
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("description")]
    public string Description { get; set; }
    [JsonProperty("icon-url")]
    public string IconUrl { get; set; }
    [JsonProperty("price")]
    public int Price { get; set; }
}

I try to get the items inside the Menu, so far I could reach lets say Name of a Menu without a problem by using a foreach loop, but when I do a foreach inside a foreach I can't reach the Item itself.

I have to following loop:

foreach (var desoi in deserializedOrderInfo.Menu)
{
  foreach (var desoiItem in desoi.Items)
  {
    var clickableBoxv = new RoundedBackgroundBoxView
    {
      CornerRadius = 6,
      Margin = new Thickness(0, 5, 0, 5),
      Index = HttpUtility.HtmlDecode(desoiItem.Id),
      Name = HttpUtility.HtmlDecode(desoiItem.Name),
      Price = HttpUtility.HtmlDecode(desoiItem.Price),
    };
  }
}

Using this I get the error that there is no "Price" in Menu, while I thought that "desoiItem" would be the Items list, not the Menu List.

How can I use the info inside the Items Class?

Upvotes: 0

Views: 45

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65156

I get the feeling this is supposed to say List<Items> instead:

public List<Menu> Items { get; set; }

(And I would rename the class to MenuItem or something, having a plural class name such as Items for a single item is weird)

Upvotes: 2

Related Questions