添加履行到Shopify订单



这是我在添加履行到Shopify订单的代码,但转换的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; }
    }

这些是履约和行项目的模型。

提前感谢您给我的建议和帮助。

这个适合我:

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

显然你需要填写你的具体数据

像下面这样修改你的类。

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
      }
    ]
  }
}

试试下面的

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

相关内容

  • 没有找到相关文章

最新更新