简体   繁体   中英

How to set a array of object inner a class property

I need to create an object to return a json like this:

{
        "Name": "John",
        "Age": 25,
        "Products": [
            {
                "Code": "1",
                "Price": 400.00
            },
            {
                "Code": "2",
                "Price": 100.00
            },
            {
                "Code": "32",
                "Price": 250.00
            },
        ],
}

How can I do this? :/ I just need to create a object and convert to json... I know how to convert, but dont know how to populate the Products model with a "object array"

My Model Class:

public class Customer
{
    public string Name { get; set; }
    public string Age { get; set; }
    public Products[] Products { get; set; }
}

public class Products
{
    public string Code { get; set; }
    public string Price { get; set; }
}

My program to populate the data: (Here you can see one exemple populating the Customers model 10 times and the Product model 3 times)

            List<Object> customerList = new List<Object>();
            List<Object> productList = new List<Object>();

            for (int i = 0; i < 10; i++)
            {
                Customer customer = new Customer();

                customer.Name = "Pedro";
                customer.Age = "20";

                for (int ii = 0; ii < 3; ii++)
                {
                    Product product = new Product();

                    product.Code = "1";
                    product.Price = "400.00";

                    productList.Add(product);
                }

                customer.Product = // Why I need to do here?

                customerList.Add(customer);
}

You already have the products in a List, so all you need is to convert it to an array to match the property type:

customer.Products = productList.ToArray();

and of course, you should change declaration of to:

List<Product> productList = new List<Product>();

if you don't want to change the declaration for any reason?: you can do it like:

customer.Products = productList.Select(x => (Product)x).ToArray();

The short answer to this:

customer.Product = // Why I need to do here?

Is simply that you need to add your products as a Product[] (because that's how it's defined in the Customer class), which we can do by casting the items from the list to a Product (so we get strongly typed objects instead of just Object objects) and use the System.Linq extension method, ToArray() to create an array from the list:

customer.Product = productList.Select(p => (Product)p).ToArray();

// Note that we should clear our list for the next iteration
productsList.Clear();

Some more things to consider:

  1. Classes should not be named with a plural unless they're collections, so your Products class should be named Product .

  2. You should avoid using lists of object when you have strongly-typed classes available, so List<Object> customerList should be List<Customer> customers .

  3. You're populating lists of the same data over and over again. I assume it's because this is just an example, but wanted to let you know just in case.

Your existing code could be modified to something like:

List<Customer> customers = new List<Customer>();

for (int i = 0; i < 10; i++)
{
    // Create a new customer
    Customer customer = new Customer
    {
        Name = "Pedro",
        Age = "20",
    };

    // Create an array of products since Customer expects this type
    Product[] products = new Product[3];
    for (int j = 0; j < products.Length; j++)
    {
        products[j] = new Product
        {
            Code = "1",
            Price = "400.00"
        };
    }

    // Add the array to our customer
    customer.Products = products;

    // Add our customer to the list
    customers.Add(customer);
}

Or we could use pure Linq, and select our data using Enumerable.Range as a "loop" construct:

List<Customer> customers = Enumerable.Range(0, 10).Select(c =>
    new Customer
    {
        Name = "Pedro",
        Age = "20",
        Products = Enumerable.Range(0, 3).Select(p => new Product
        {
            Code = "1",
            Price = "400.00"
        }).ToArray()
    }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM