Xamarin/C# POST 到 RESTful api

Xamarin/C# POST to RESTful api

如果这是一个愚蠢的问题,我深表歉意,我被投入到一个项目中,之前的 C#/Xamarin 知识很少,而且我已经用这个问题撞墙一段时间了。

所以...

我正在尝试 post 调用 SagePay API (https://test.sagepay.com/documentation/#card-identifiers)

我一直在访问我们的 API 我已经访问了另一个 SagePay API 没问题,

我遇到的问题是这个调用是 'nested' json(对不正确的术语表示歉意)

如何以这种格式提交 POST

{
"cardDetails":
    {
        "cardholderName": "SAM JONES",
        "cardNumber": "4929000000006",
        "expiryDate": "0320",
        "securityCode": "123"
    }
}

你基本上将 JSON 表示为一个对象并将其包装起来

public class CardDetails
{
    public string cardholderName { get; set; }
    public string cardNumber { get; set; }
    public string expiryDate { get; set; }
    public string securityCode { get; set; }
}

public class YourObject
{
    public CardDetails cardDetails { get; set; }
}

您在其中创建了对象 link (json2csharp):

public class CardDetails
{
    public string cardholderName { get; set; }
    public string cardNumber { get; set; }
    public string expiryDate { get; set; }
    public string securityCode { get; set; }
}

public class RootObject
{
    public CardDetails cardDetails { get; set; }
}

序列化(JSON.Net):

var cardIdentifier = new RootObject{
    cardDetails = new CardDetails{
        cardholderName = "EdSF", 
        cardNumber = "4111111111111111", 
        expiryDate = "0320", 
        securityCode = "123"
    }
};


Console.WriteLine(JsonConvert.SerializeObject(cardIdentifier));

结果:

{
  "cardDetails": {
    "cardholderName": "EdSF",
    "cardNumber": "4111111111111111",
    "expiryDate": "0320",
    "securityCode": "123"
  }
}

Hth....