Q:

Assign values to dynamic number of sub-classes before serializing to JSON

I am integrating with a courier that requires me to pass box dimensions for each box in my consignment to their API in JSON format. I am able to set individual properties like RecipientName, but am not sure how to pass the box details for the varying number of boxes for each consignment.
The JSON needs to look like this (example is for a 2 box consignment):
{
    "RecipientName": "Joe Bloggs",
    "Packages" : [{
        "boxNumber": "1",
        "boxHeight": 1.55,  
        "boxLength": 1.55,
        "boxWidth": 1.55
    },
    {
        "boxNumber": "2",
        "boxHeight": 2.55,  
        "boxLength": 2.55,
        "boxWidth": 2.55
    }]
}

I have built 2 classes, one that describes the structure of the JSON, and another that contains the method to serialize the JSON.
My JSON structure class looks like this (I have used a List because I have read that arrays are a fixed length, and because the number of boxes with vary I cannot use arrays):
public class API_JSON
{
    public class Rootobject
    {
        public string RecipientName { get; set; }
        public List<Package> Packages { get; set; }
    }

    public class Package
    {
        public string boxNumber { get; set; }
        public double boxHeight { get; set; }
        public double boxLength { get; set; }
        public double boxWidth { get; set; }
    }
}

And my API methods class looks like this:
public class API_Methods
{
    public string recipientName;
    public List<string> boxnumber;
    public List<double> boxHeight;
    public List<double> boxLength;
    public List<double> boxWidth;

    public Boolean SubmitConsignment(out string JSONData)
    {
        var NewRequestObject = new API_JSON.RootObject
        {
            Recipient = recipientName,
            Packages = new API_JSON.Package
            {
                foreach (string item in ContainerNumber)
                {
                    boxNumber=???,
                    boxHeight=???,
                    boxLength???=,
                    boxWidth=???
                }
            }
        }

        string JSONData = JsonConvert.SerializeObject(NewRequestObject);
        return true;
    }
}

I am then instantiating the object, setting its public variables, then running the method list this:
API_Methods myObject = new API_Methods();
myObject.recipientName;
myObject.boxnumber.Add(1);
myObject.boxnumber.Add(2);
myObject.boxHeight.Add(1.55);
myObject.boxHeight.Add(2.55);
myObject.boxLength.Add(1.55);
myObject.boxLength.Add(2.55);
myObject.boxWidth.Add(1.55);
myObject.boxWidth.Add(2.55);

bool test = API_Methods.SubmitConsignment(out JSON);

My problem is with the foreach loop - I know the code is incomplete - but I was hoping to iterate through the lists, but even with an empty foreach loop it appears to be the wrong place to put the loop as I start getting syntax errors about an expected "}"

A:

You're actually overcomplicating this for yourself - create complete package objects, and add them to the List Packages, and then pass the rootobject to the serializer.
The error you are getting is because you are not correctly initializing / filling your Packages List.  Your object is invalid, hence the serializer is throwing exceptions.
This will be a lot easier for you if you create some constructors for your objects, something like this:
public Package(number, height, length, width)
{
    boxNumber = number;
    boxHeight = height;
    //rest of your properties here in same format
}

You can then also make your setters private in the class, if you wish.
You can then easily create your package objects:
var package1 = new Package(10, 10, 10, 10);

This should make it a lot easier to create your list of boxes to put in your rootObject.
You can add each package to the packages list (individually or within a foreach loop):
Packages.Add(package1)

Or you could even start getting more concise:
Packages.Add(new Package(10,10,10,10));

You want to separate your concerns more to help keep this clear - so I'd recommend you fully construct your rootObject, add the packages to the list in one class (your 3rd code snippet), and then serialize it another (your 2nd code snippet).

Edit:
I think you'd find it easier to refactor your code somewhat:
1) Have a public rootobject in your Json_Api class, with get; set;.  Get rid of the box collections.  Get rid of your foreach loop from here too.
public class API_Methods
{

public rootObject RootObject { get; set; }

public Boolean SubmitConsignment(out string JSONData)
{
    string JSONData = JsonConvert.SerializeObject(NewRequestObject);
    return true;
}

}
2) Set the properties of this rootobject outside this class (where you currently initialize your objects).  Add the New Package()s to Packages list here too.
API_Methods myObject = new API_Methods();
myObject.RootObject.recipientName = "NAME";
myObject.RootObject.Packages.Add(new Package(10,10,10,10);
myObject.RootObject.Packages.Add(new Package(20,20,20,20);

bool test = API_Methods.SubmitConsignment(out JSON);

3) Call the API method next, it should return a serialized version of the wholerootobject, including your packages.

Just a side note, it would be more conventional to send the RootObject as a parameter to the API, and return the Json string object back.  

