0

I'm trying to get the "Last" Object from this api in C#: https://bittrex.com/api/v1.1/public/getmarketsummary?market=usdt-btc

I already have the script to get the array into my C# code but i don't know how to get this Object, i have googled around for help and i found things like these: https://www.codementor.io/andrewbuchan/how-to-parse-json-into-a-c-object-4ui1o0bx8 but it did not work for me.

EDIT The working version is here: 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
    }
6
  • 2
    maybe show us your code Commented Nov 30, 2017 at 12:52
  • pastebin.com/LXnQ2UJS @Green thats how far i've came with it Commented Nov 30, 2017 at 12:53
  • @Green sorry first question didnt know that was possible Commented Nov 30, 2017 at 12:57
  • 1
    This how you learn(: and you even already got a nice answer Commented Nov 30, 2017 at 13:00
  • @Green do you maybe know whats wrong here? dotnetfiddle.net/UlxOfK Commented Nov 30, 2017 at 13:40

1 Answer 1

3

You can use 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; }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks. But im getting this error: gyazo.com/021baab143a28ddf5da7a2cbd05f6796 at this line double price = root.Results.Last();
@FluffyMe420 did you include using System.Linq;?
Indeed, you where a few seconds faster. ;-) As an extra: if you want to use other naming for the properties than the Json fields, you can use attributes. [DataMember(Name = "last", Order = 1)] public double Last { get; set; }
@aloisdg i fixed it but now i want to put it in a label (price.Text = price;) But it tells me that it cant convert to a string
@aloisdg there is an error. also in your online version.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.