thank you in advance for your contributions. I am passing a JSON string to a .NET controller and parsing it in a C# class. Here is my JSON..
{"CusEmail":"[email protected]","Name":"Foo Bar",
"Items":[{"ItemId":1234,"ItemDesc":"Item-1234"},{"ItemId":5678,"ItemDesc":"Item-5678"}]
}
Here is a snippet of my C# class defining the JSON variables (not including the nested array).
public class CreateOrderRequest
{
public string CusEmail { get; set; }
public string Name { get; set; }
}
And here is a snippet of my C# class that is parsing the JSON
public JsonResult CreateShipTo(CreateOrderRequest createOrderRequest)
{
... parsing code here
}
Currently, all of this code works but now I need to access that nested array "Items" in the JSON. How do I declare that correctly in the code above? I tried Array but that did not work.
Todd WunschTodd Wunsch
1 Answer 1
A List will work:
public class Item
{
public int ItemId { get; set; }
public string ItemDesc { get; set; }
}
public class WithItems
{
public List<Item> Items { get; set; }
}
answered Apr 3, 2021 at 22:53
Comments
lang-cs
public List<Item> Items { get; set; }
toCreateOrderRequest
whereItem
looks likepublic class Item { public long ItemId { get; set; } public string ItemDesc { get; set; } }
.I tried Array but that did not work
- Can you include what you tried? How did it not work? Did you get an error?