Adding Fulfillment to Shopify orders

This is my code in adding Fulfillment to Shopify orders but the converted json is not as expected.

Fullfillment product = new Fullfillment();
product.status = "success";
product.tracking_number = orderSent.TrackingNo;

List<LineItems> items = new List<LineItems>();
foreach (var item in orderSent.OrderLines)
{
   LineItems line = new LineItems();
   line.id = item.ProductName;
   items.Add(line);
}

var json = JsonConvert.SerializeObject(product);
json = "{ \"fulfillment\": " + json + "}";

var json1 = JsonConvert.SerializeObject(items);
json = json + "{ \"line_items\": " + json1 + "}";

And this the converted json from this code:

{ "fulfillment": {
    "id":0,
    "status":"success",
    "tracking_number":"xxxx12222",
    }}{ 
    "line_items": [
    {
    "id":"1234566645"
    }
    ]
}

How can I turned like this:

{
  "fulfillment": {
    "tracking_number": null,
    "line_items": [
      {
        "id": 466157049,
        "quantity": 1
      }
    ]
  }
}

Model:

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class Fullfillment
    {
        [JsonProperty(PropertyName = "id")]
        public long id { get; set; }

        [JsonProperty(PropertyName = "status")]
        public string status { get; set; }

        [JsonProperty(PropertyName = "tracking_number")]
        public string tracking_number { get; set; }
    }

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class LineItems
    {
        [JsonProperty(PropertyName = "id")]
        public string id { get; set; }
    }

These are the models for Fulfillment and Line Items.

Thank you in advance for giving advices and help.

Answers


This works for me:

var json = JsonConvert.SerializeObject(new
{
    fullfillment = new
    {
        product.tracking_number,
        line_items = items.Select(x => new
        {
            x.id,
            quantity = 1
        })
    }
});

That gives me:

{
    "fullfillment" : {
        "tracking_number" : "xxxx12222",
        "line_items" : [{
                "id" : "1234566645",
                "quantity" : 1
            }
        ]
    }
}

I started with this code to build up the JSON above:

Fullfillment product = new Fullfillment();
product.status = "success";
product.tracking_number = "xxxx12222";

List<LineItems> items = new List<LineItems>();

LineItems line = new LineItems();
line.id = "1234566645";
items.Add(line);

Obviously you need to fill in your specific data.


Change your classes like below.

public class Rootobject
{
    public Fulfillment fulfillment { get; set; }
}

public class Fulfillment
{
    public string tracking_number { get; set; }
    public Line_Items[] line_items { get; set; }
}

public class Line_Items
{
    public string id { get; set; }
    public int quantity { get; set; }
}

public class JsonTest
{
    public void Test()
    {
        var root = new Rootobject();
        root.fulfillment = new Fulfillment();
        root.fulfillment.tracking_number = "xxxx12222";
        root.fulfillment.line_items = new List<Line_Items>() { new Line_Items() { id = "1234566645", quantity = 1 } }.ToArray();

        var json = JsonConvert.SerializeObject(root);
        Console.WriteLine(json);
    }
}

This will give you this json.

{
  "fulfillment": {
    "tracking_number": "xxxx12222",
    "line_items": [
      {
        "id": "1234566645",
        "quantity": 1
      }
    ]
  }
}

Need Your Help

Showing edittext obliquely in android

android android-widget android-edittext

I have an EditText, which generally shows parallel to the screen X-axis. I want to show it obliquely (around 45 degree to horizontal axis). Is it possible to do this in Android. Please guide me in a

Are loops with and without parenthesis handled differently in C?

c debugging loops for-loop cuda

I was stepping through some C/CUDA code in the debugger, something like: