【问题标题】:C# parse JSON API arrayC# 解析 JSON API 数组
【发布时间】:2017-11-30 12:50:08
【问题描述】:

我正在尝试从 C# 中的这个 api 获取“最后一个”对象:https://bittrex.com/api/v1.1/public/getmarketsummary?market=usdt-btc

我已经有了将数组放入我的 C# 代码的脚本,但我不知道如何获取此对象,我在 Google 上四处寻找帮助,我发现了以下内容:https://www.codementor.io/andrewbuchan/how-to-parse-json-into-a-c-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
    }

【问题讨论】:

  • 也许向我们展示你的代码
  • pastebin.com/LXnQ2UJS @Green 这就是我已经走了多远
  • @Green 抱歉第一个问题不知道这是可能的
  • 这就是你学习的方式(:你甚至已经得到了一个很好的答案
  • @Green 你知道这里有什么问题吗? dotnetfiddle.net/UlxOfK

标签: c# arrays json


【解决方案1】:

您可以使用 JSON.NET:

Try it online

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; }
}

【讨论】:

  • 谢谢。但我得到这个错误:gyazo.com/021baab143a28ddf5da7a2cbd05f6796 在这一行 double price = root.Results.Last();
  • @FluffyMe420 是否包含using System.Linq;
  • 确实,你的速度快了几秒钟。 ;-) 另外:如果您想对属性使用 Json 字段以外的其他命名,则可以使用属性。 [DataMember(Name = "last", Order = 1)] public double Last { get;放; }
  • @aloisdg 我修复了它,但现在我想把它放在标签中(price.Text = price;)但它告诉我它不能转换为字符串
  • @aloisdg 出现错误。也在您的在线版本中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 2016-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多