添加实现以Shopify订单

时间:2016-09-23 03:04:15

标签: c# json shopify fulfillment

这是我在为Shopify订单添加Fulfillment时的代码,但转换的json不符合预期。

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 + "}";

这是来自此代码的转换后的json:

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

我怎么能这样:

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

型号:

[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; }
    }

这些是Fulfillment和Line Items的模型。

提前感谢您提供建议和帮助。

3 个答案:

答案 0 :(得分:1)

这对我有用:

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

这让我:

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

我从这段代码开始构建上面的JSON:

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);

显然,您需要填写您的具体数据。

答案 1 :(得分:1)

更改您的课程,如下所示。

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);
    }
}

这会给你这个json。

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

答案 2 :(得分:-1)

尝试以下

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);
    }
}