2016-09-23 78 views
1

这是我向Shopify订单添加配送的代码,但转换后的json与预期不符。向Shopify订单添加配送

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

这些都是模型用于履行和行项目。

非常感谢您提供建议和帮助。

+0

这段代码是C#吗? – Enigmativity

+0

“Fullfillment”和“LineItems”的类定义在哪里? – Enigmativity

回答

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

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

+0

我也会有这样的JSON数据: – Jen143

+0

{ “应验”:{ “ID”:0, “状态”: “成功”, “TRACKING_NUMBER”: “000000000000”, “line_items”:[{ “id”:“0000000” } ] } } – Jen143

+0

得到此响应:远程服务器返回错误:(406)不可接受。 – Jen143

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

谢谢你。这将在未来非常有用。 – Jen143