C# 如何忽略JSON POST请求的一部分
我实际上是从一个API获取信息来进行自动交易。 代码如下:C# 如何忽略JSON POST请求的一部分,c#,json,post,C#,Json,Post,我实际上是从一个API获取信息来进行自动交易。 代码如下: string webAddr = "https://shapeshift.io/sendamount"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json; charset=utf-8"; h
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);
当它出现时,另一个类正在获取这些信息并将其放入变量中。感谢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; }
}
问题是它不起作用。我认为这是从响应的一开始就取得的成功,API中的另一个选项非常有效,我不知道如何绕过它来获取其他信息。您需要将所有JSON属性移动到另一个名为“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; }
}
提示:VisualStudio有一个很酷的功能,可以从JSON消息中获取对象。转到类编辑器,然后从“编辑”菜单中选择选项“粘贴特殊->粘贴JSON作为类”
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; }
}