Square Connect API项目创建语法

时间:2014-10-02 01:53:38

标签: c# json api connect square

我试图通过C#将新项目创建发布到测试商店,但我不确定语法应该如何阅读。 Square Connect API至少需要一个变体来创建新项目,但我不确定如何将其添加到JSON主体。这就是我所拥有的,但我不确定如何完成它。

var client = new RestSharp.RestClient();
var post = new RestRequest("https://connect.squareup.com/v1/me/items", Method.POST);
post.RequestFormat = DataFormat.Json;
post.AddHeader("Authorization", String.Format("Bearer {0}", testtoken));
post.AddBody(new { name = testname, variations = ???? });

此代码有效,但返回项目的响应必须包含至少一个变体。我意识到这一点,但不知道如何写它,或者它是否可能。

我并不反对走另一条路。

已编辑,可从Square文档中添加示例请求正文:

   {
       "name": "Milkshake",
       "description": "It's better than yours",
       "visibility": "PRIVATE",
       "category_id": "36ac7016-3a4e-4934-81f1-9057ac613f2y",
       "variations": [
       {
         "name": "Small",
         "pricing_type": "FIXED_PRICING",
         "price_money": {
           "currency_code": "USD",
           "amount": 400
       },
        "sku": "123"
      }
   ]
}

1 个答案:

答案 0 :(得分:0)

这样的事情应该以正确的格式序列化为JSON:

post.AddBody(new {
    name = testname,
    variations = new object[] {
        new {
            name = "Small",
            pricing_type = "FIXED_PRICING",
            price_money = new {
                currency_code = "USD",
                amount = 400
            }
        }
    },
    sku = "123"
});