0

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.

asked Apr 3, 2021 at 22:02
2
  • 1
    Use one of the tools from How to auto-generate a C# class file from a JSON string to generate a data model for you. You will need to add a public List<Item> Items { get; set; } to CreateOrderRequest where Item looks like public class Item { public long ItemId { get; set; } public string ItemDesc { get; set; } }. Commented Apr 3, 2021 at 22:29
  • I tried Array but that did not work - Can you include what you tried? How did it not work? Did you get an error? Commented Apr 4, 2021 at 1:46

1 Answer 1

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.