我实际上是从API信息中获取自动交易。 这是代码:
string webAddr = "https://shapeshift.io/sendamount";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{ \"amount\" : \"1.08518719\", \"withdrawal\" : \"***SNIP***\", \"pair\" : \"eth_xmr\" }";
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(responseText);
Console.WriteLine(responseText);
执行后,它给了我这个:
{"success":{"orderId":"6d8bcd05-57c5-4b8d-a4b5-c7080f3c84fd","pair":"eth_xmr","withdrawal":"***SNIP***","withdrawalAmount":"1.08518719","deposit":"0x93163985f6b4d4687ac7bc63d54016ed2d5f6aec","depositAmount":"0.34529822","expiration":1507244337386,"quotedRate":"3.20067447","maxLimit":16.79846053,"apiPubKey":"shapeshift","minerFee":"0.02"}}
当出现这种情况时,另一个类正在获取这些信息以将它们变为变量(感谢Christos获取该代码)
public class ApiResponse
{
[JsonProperty("orderId")]
public static string orderId { get; set; }
[JsonProperty("pair")]
public static string pair { get; set; }
[JsonProperty("withdrawal")]
public static string withdrawal { get; set; }
[JsonProperty("withdrawalAmount")]
public static string withdrawalAmount { get; set; }
[JsonProperty("deposit")]
public static string deposit { get; set; }
[JsonProperty("depositAmount")]
public static string depositAmount { get; set; }
[JsonProperty("expiration")]
public static string expiration { get; set; }
[JsonProperty("quotedRate")]
public static string quotedRate { get; set; }
[JsonProperty("maxLimit")]
public static string maxLimit { get; set; }
[JsonProperty("apiPubKey")]
public static string apiPubKey { get; set; }
[JsonProperty("minerFee")]
public static string minerFee { get; set; }
}
问题是它不起作用。我认为它来自响应开头的“SUCCESS”(来自API的另一个选项,它完全正常工作),我不知道如何“绕过”它只获取其他信息。
答案 0 :(得分:1)
您需要将所有JSON属性移动到另一个名为“Success”的类,并且此Success类需要位于您的API响应类中,因为这些属性包含在“success”对象中。它看起来像这样:
public class ApiResponse
{
public Success success { get; set; }
}
public class Success
{
[JsonProperty("orderId")]
public static string orderId { get; set; }
[JsonProperty("pair")]
public static string pair { get; set; }
[JsonProperty("withdrawal")]
public static string withdrawal { get; set; }
[JsonProperty("withdrawalAmount")]
public static string withdrawalAmount { get; set; }
[JsonProperty("deposit")]
public static string deposit { get; set; }
[JsonProperty("depositAmount")]
public static string depositAmount { get; set; }
[JsonProperty("expiration")]
public static string expiration { get; set; }
[JsonProperty("quotedRate")]
public static string quotedRate { get; set; }
[JsonProperty("maxLimit")]
public static string maxLimit { get; set; }
[JsonProperty("apiPubKey")]
public static string apiPubKey { get; set; }
[JsonProperty("minerFee")]
public static string minerFee { get; set; }
}
提示:Visual Studio有一个很酷的功能,可以从JSON消息中获取对象。转到您的班级编辑器,然后从编辑菜单中选择选项作为选择性粘贴 - &gt;将JSON粘贴为类。