[英]C# parse JSON API array
我正在嘗試從C#中的此api獲取“最后一個”對象: https : //bittrex.com/api/v1.1/public/getmarketsummary?market= usdt- btc
我已經有了將數組放入C#代碼的腳本,但是我不知道如何獲取該對象,我在Google周圍四處尋求幫助,並且發現了類似以下內容: https : //www.codementor.io/andrewbuchan/如何將json解析為ac-object-4ui1o0bx8,但它對我不起作用。
編輯工作版本在這里: http : //dotnetfiddle.net/5VFof9
//GET api array
string GET(string url) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
try {
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream()) {
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
} catch (WebException ex) {
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream()) {
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
// log errorText
}
throw;
}
}
decimal coin_price() {
String array = GET("https://bittrex.com/api/v1.1/public/getmarketsummary?market=usdt-btc");
decimal price = 0;
//return price in price var
return price;
}
private void Form1_Load(object sender, EventArgs e) {
price.Text = ""; //Display price here
}
您可以使用JSON.NET:
public static void Main()
{
var url = "https://bittrex.com/api/v1.1/public/getmarketsummary?market=usdt-btc";
// for a simple get, use WebClient
var json = new WebClient().DownloadString(url);
// learn more on https://www.newtonsoft.com/json
var root = JsonConvert.DeserializeObject<RootObject>(json);
// root.Results.Last() is your last item
// learn more on Last() at https://msdn.microsoft.com/fr-fr/library/bb358775(v=vs.110).aspx
Console.WriteLine(root.Result.Last().TimeStamp);
}
// generated with http://json2csharp.com/ (VS has a builtin with edit>past special)
public class Result
{
public string MarketName { get; set; }
public double High { get; set; }
public double Low { get; set; }
public double Volume { get; set; }
public double Last { get; set; }
public double BaseVolume { get; set; }
public DateTime TimeStamp { get; set; }
public double Bid { get; set; }
public double Ask { get; set; }
public int OpenBuyOrders { get; set; }
public int OpenSellOrders { get; set; }
public double PrevDay { get; set; }
public DateTime Created { get; set; }
}
public class RootObject
{
public bool Success { get; set; }
public string Message { get; set; }
public List<Result> Result { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.