如何将这个特定的 JSON 解析为字符串?
How to parse this specific JSON to String?
我已经尝试了所有可能的解决方案来尝试使用 Newtonsoft.JSON 和 System.Web.Script.Serialization 来解析这个 JSON,但无论我使用什么方法,我都会一个接一个地出错。
我正在尝试使用 https://api.kucoin.com/api/v1/market/stats?symbol=BTC-USDT API 并且我正在尝试从中解析值以便稍后引用它们。
很难显示我的代码,因为我尝试了很多不同的方法,我不知道哪里出了问题。我认为这可能是由于 API 本身的一些奇怪的格式,但我不太确定我将如何处理它。我该怎么做?
代码示例:https://www.toptal.com/developers/hastebin/raw/uxomitoweb
试试这个。它在 Visual Studio 中进行了测试并且工作正常
using (var client = new HttpClient())
{
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
var baseAddress = @"https://api.kucoin.com";
var api = @"api/v1/market/stats?symbol=BTC-USDT";
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Add(contentType);
var response = await client.GetAsync(api);
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<Data>(json);
}
}
类
public partial class Data
{
[JsonProperty("code")]
public long Code { get; set; }
[JsonProperty("data")]
public DataClass DataData { get; set; }
}
public partial class DataClass
{
[JsonProperty("time")]
public long Time { get; set; }
[JsonProperty("symbol")]
public string Symbol { get; set; }
[JsonProperty("buy")]
public string Buy { get; set; }
[JsonProperty("sell")]
public string Sell { get; set; }
[JsonProperty("changeRate")]
public string ChangeRate { get; set; }
[JsonProperty("changePrice")]
public string ChangePrice { get; set; }
[JsonProperty("high")]
public string High { get; set; }
[JsonProperty("low")]
public long Low { get; set; }
[JsonProperty("vol")]
public string Vol { get; set; }
[JsonProperty("volValue")]
public string VolValue { get; set; }
[JsonProperty("last")]
public string Last { get; set; }
[JsonProperty("averagePrice")]
public string AveragePrice { get; set; }
[JsonProperty("takerFeeRate")]
public string TakerFeeRate { get; set; }
[JsonProperty("makerFeeRate")]
public string MakerFeeRate { get; set; }
[JsonProperty("takerCoefficient")]
public long TakerCoefficient { get; set; }
[JsonProperty("makerCoefficient")]
public long MakerCoefficient { get; set; }
}
````
我已经尝试了所有可能的解决方案来尝试使用 Newtonsoft.JSON 和 System.Web.Script.Serialization 来解析这个 JSON,但无论我使用什么方法,我都会一个接一个地出错。
我正在尝试使用 https://api.kucoin.com/api/v1/market/stats?symbol=BTC-USDT API 并且我正在尝试从中解析值以便稍后引用它们。
很难显示我的代码,因为我尝试了很多不同的方法,我不知道哪里出了问题。我认为这可能是由于 API 本身的一些奇怪的格式,但我不太确定我将如何处理它。我该怎么做?
代码示例:https://www.toptal.com/developers/hastebin/raw/uxomitoweb
试试这个。它在 Visual Studio 中进行了测试并且工作正常
using (var client = new HttpClient())
{
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
var baseAddress = @"https://api.kucoin.com";
var api = @"api/v1/market/stats?symbol=BTC-USDT";
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Add(contentType);
var response = await client.GetAsync(api);
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<Data>(json);
}
}
类
public partial class Data
{
[JsonProperty("code")]
public long Code { get; set; }
[JsonProperty("data")]
public DataClass DataData { get; set; }
}
public partial class DataClass
{
[JsonProperty("time")]
public long Time { get; set; }
[JsonProperty("symbol")]
public string Symbol { get; set; }
[JsonProperty("buy")]
public string Buy { get; set; }
[JsonProperty("sell")]
public string Sell { get; set; }
[JsonProperty("changeRate")]
public string ChangeRate { get; set; }
[JsonProperty("changePrice")]
public string ChangePrice { get; set; }
[JsonProperty("high")]
public string High { get; set; }
[JsonProperty("low")]
public long Low { get; set; }
[JsonProperty("vol")]
public string Vol { get; set; }
[JsonProperty("volValue")]
public string VolValue { get; set; }
[JsonProperty("last")]
public string Last { get; set; }
[JsonProperty("averagePrice")]
public string AveragePrice { get; set; }
[JsonProperty("takerFeeRate")]
public string TakerFeeRate { get; set; }
[JsonProperty("makerFeeRate")]
public string MakerFeeRate { get; set; }
[JsonProperty("takerCoefficient")]
public long TakerCoefficient { get; set; }
[JsonProperty("makerCoefficient")]
public long MakerCoefficient { get; set; }
}
````