Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, so i want to get bitcoin price from the json url and see it in a label in my form.

url: https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT[^]

this is my code for the form:

<pre>using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace Manager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT

            var BPrice = @"{https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT}";

            BTCPrice_Label.Text = BPrice;

        }

    }

}


and this is the code for the class that i made :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Manager
{
    class PriceClass
    {
        public class Rootobject
        {
            public string symbol { get; set; }
            public string price { get; set; }
        }


    }
}


What I have tried:

i made a class for it with the code

public string price { get; set; }


but i don't know what to do after that, i searched a lot in google but they all show the result in list or gridview and etc ....
Posted
Updated 7-Aug-20 2:53am
v5

Google "c# deserialise json" for articles on how to convert json to c# classes

Working With JSON String In C#[^]

https://www.codementor.io/@andrewbuchan/how-to-parse-json-into-a-c-object-4ui1o0bx8[^]

once you have it as an object simply do

lblMyLabel.Text = myJSONObject.price;
 
Share this answer
 
Comments
brandon1999 7-Aug-20 8:17am    
thanks for the answer, i tried

var BPrice = @"{""Price"":20}";

BTCPrice_Label.Text = BPrice;

but it shows {""Price"":20} what should i do?!
BillWoodruff 7-Aug-20 10:02am    
+5
found the solution!

the code should be like this

string url = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT";
            using (WebClient wc = new WebClient())
            {
                var json = wc.DownloadString(url);
                JavaScriptSerializer oJS = new JavaScriptSerializer();
                PriceClass obj = new PriceClass();
                obj = oJS.Deserialize<PriceClass>(json);

                BTCPrice_Label.Text = obj.price;

            }




and the class should be like this

using System;


        public class PriceClass
        {
            public string symbol { get; set; }
            public string price { get; set; }
        }
 
Share this answer
 
Try this:
C#
static void Main(string[] args)   
{  
      string jsonData = @ "{'FirstName': 'Sandeep', 'LastName': 'Mewara'}";  
      var sampleObject = JsonConvert.DeserializeObject<SampleClass>(jsonData);  
      Console.WriteLine(sampleObject.FirstName + " " + sampleObject.LastName));  
      // Do whatever you want with the sampleObject => use it to assign a label as you need.
}  

public class SampleClass  
{  
    public string FirstName { get; set; }  
    public string LastName { get; set; }
}

UPDATE:
I missed adding a sentence here that Deserialization is what you need to convert JSON into an object. Once done, you can use it as per your need. Example above demonstrates that. Reference: How to serialize and deserialize JSON using C# - .NET | Microsoft Docs[^]
 
Share this answer
 
v4
Comments
brandon1999 7-Aug-20 8:25am    
thank you for the answer but i couldn't figure it out ( i'm a beginner )

i added the code i got and error : only assignment, call ... and new object expressions can be used as a statement

i don't understand what am i doing wrong?!

by the way english is my second language
BillWoodruff 7-Aug-20 10:01am    
My vote of #1. This is irrelevant to the OP's question.
Sandeep Mewara 7-Aug-20 10:17am    
How so? Please share - OP wanted to know how to extract json data so that it can be shown in a label.

Sample show deserialization that gives that option to use.
[no name] 8-Aug-20 12:03pm    
Have a 5 to compensate the uncommented 1
Sandeep Mewara 8-Aug-20 12:34pm    
:thumbsup: thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900