doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
733 stars 126 forks source link

How can I assign json data to variables? #327

Open Rhemecoh opened 1 year ago

Rhemecoh commented 1 year ago

Hello,

How can I assign json data to variables?

client.On("price_changed", response =>
{
Console.WriteLine(response);
});

image

My all code:

        public static async Task Main(string[] args)
        {
            var client = new SocketIO("wss://www.haremaltin.com:2088");

            client.On("price_changed", (Data) => // Argument can be used without type.
            {

            });

            client.On("price_changed", response =>
            {

                Console.WriteLine(response);

            });

            client.OnConnected += async (sender, e) =>
            {
                // Emit a string
                await client.EmitAsync("");
                Console.WriteLine("Connected");
                Console.ReadKey();

                // Emit a string and an object
            };
            await client.ConnectAsync();
            Console.ReadKey();
        }

What I want to do is to assign the USDTRY price, the GOLD price, the EURTRY price to the variables and print them one after the other. Example: string USDTRY = USDTRYprice(26.1200); string EURTRY = EURTRYprice(28.59100); strring GOLD = GOLDprice(1622.370);

Console.WriteLine("USDTRY Price= "+ USDTRY) Console.WriteLine("EURTRY Price= "+ EURTRY) Console.WriteLine("GOLD Price= "+ GOLD)

Thank you for your help

Rhemecoh commented 1 year ago

I solved most of the problem. The data from the socket comes as an array. I need to convert it to Object. How can I do that?

boxofyellow commented 1 year ago

@Rhemecoh https://www.nuget.org/packages/Newtonsoft.Json is a very popular package for serializing and deserializing json.

On1ty commented 8 months ago

It's much better if you create a class for your response and use JsonConvert.SerializeObject of Newtonsoft.Json