2016-09-23 1 views
1

Ceci est mon code lors de l'ajout de commandes Fulfillment to Shopify, mais le json converti n'est pas conforme aux attentes.Ajout de commandes aux commandes 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 + "}"; 

Et ce JSON converti à partir de ce code:

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

Comment puis-je tourné comme ceci:

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

Modèle:

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

Ce sont les modèles pour l'exécution et les postes.

Merci d'avance pour vos conseils et votre aide.

+0

Est-ce le code C#? – Enigmativity

+0

Où est la classe def pour 'Fullfillment' &' LineItems'? – Enigmativity

Répondre

1

Cela fonctionne pour moi:

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

Cela me donne:

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

j'ai commencé avec ce code pour construire l'JSON ci-dessus:

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

Il est évident que vous devez remplir vos données spécifiques .

+0

Je vais avoir ces données JSON: – Jen143

+0

{ "accomplissement": { "id": 0, "status": "succès", "TRACKING_NUMBER": "000000000000", "line_items": [ { "id": "0000000" } ] } } – Jen143

+0

Et obtenu la réponse suivante: Le serveur distant a renvoyé une erreur: (406) Non acceptable. – Jen143

1

Modifier vos classes comme ci-dessous.

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

Cela vous donnera ce json.

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

Merci pour ça. Ce serait très utile à l'avenir. – Jen143