简体   繁体   中英

C# add values to a Dictionary using a loop

I'm using EasyPost(shipping API) to get shipping rates.

I'm able to hard code the shipments but that doesn't help me. I need to add data based on what the user selects.

Below is the code I have. As you can see I have two shipments that are currently hard coded. I'd like to remove the hard coded shipments and add a loop around it and add shipments that way. I can pass the package type and weight myself through variables.

So if the user has 5 packages I would like to add 5 shipments.

Thanks in advance!

List<Dictionary<string, object>> shipments;

 shipments = new List<Dictionary<string, object>>() {
            new Dictionary<string, object>() {
                {"parcel", new Dictionary<string, object>() {{ "predefined_package", "MediumFlatRateBox" },  { "weight", 5 }}}
            },
            new Dictionary<string, object>() {
                {"parcel", new Dictionary<string, object>() {{ "predefined_package", "LargeFlatRateBox" },  { "weight", 15 }}}
            }
        };
parameters = new Dictionary<string, object>() {
            {"to_address", toAddress},
            {"from_address", fromAddress},
            {"reference", "USPS"},
            {"shipments", shipments}
                 };

Order order = Order.Create(parameters);

You would loop over your list of packages and add items to your list of Dictionaries.

List<Package> packages = new List<Package>();
// add your packages here ...

List<Dictionary<string, object>> shipments = new List<Dictionary<string, object>>();
foreach(var p in packages){
    shipments.Add(new Dictionary<string, object>() {
          {"parcel", 
           new Dictionary<string, object>() {
               { "predefined_package", "MediumFlatRateBox" },  
               { "weight", p.Weight }}}
            });
}

It's not quite what the question is about, but if you are trying to communicate with an API over HTTP by POSTing JSON (as I understand it is your goal to call the APIs at https://www.easypost.com/docs/api.html#shipments ), it would be more readable, and more idiomatic to use anonymously typed objects as such :

var shipments = new List<object>();
foreach(var p in packages){
    shipments.Add(new {
          parcel = new {
              predefined_package = "MediumFlatRateBox",
              weight = p.Weight
          }
    });
}

var parameters = new {
   to_address = toAddress,
   from_address = fromAddress,
   reference = USPS,
   shipments = shipments
};

(sorry about the indenting, I don't have an IDE at reach)

Also, the documentations suggests that EasyPost has a C# library for what you are trying to do, that already has all the proper types so you don't need to use Dictionaries all over the place. See: https://github.com/EasyPost/easypost-csharp :

Parcel parcel = new Parcel() {
    length = 8,
    width = 6,
    height = 5,
    weight = 10
};

Shipment shipment = new Shipment() {
    from_address = fromAddress,
    to_address = toAddress,
    parcel = parcel
};

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