1

这是我在 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 的模型。

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

4

3 回答 3

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

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

于 2016-09-23T03:12:59.283 回答
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
      }
    ]
  }
}
于 2016-09-23T03:52:06.033 回答
-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);
    }
}
于 2020-01-08T10:21:20.990 回答