-2

I have a string and I want to get some values from it.

My strings seem like:

{
    "BTC": {
        "highest_buy_bid": 3699003.04,
        "lowest_sell_bid": 3703660.94,
        "last_traded_price": 3699003.04,
        "yes_price": 3597618.7,
        "volume": {
            "max": "3750000.00",
            "min": "3577155.82",
            "volume": 43.12889007
        }
    },
    "XRP": {
        "highest_buy_bid": 95.2,
        "lowest_sell_bid": 95.3,
        "last_traded_price": 95.2,
        "yes_price": 95,
        "volume": {
            "max": "97.91",
            "min": "93.20",
            "volume": 329050.21
        }
    },
    "NEO": {
        "highest_buy_bid": 4241.3,
        "lowest_sell_bid": 4273.03,
        "last_traded_price": 4244.85,
        "yes_price": 4223.47,
        "volume": {
            "max": "4296.35",
            "min": "4178.65",
            "volume": 3988.2717
        }
    },
}

How can I get the values of: Name:=BTC,last_traded_price=3699003.04 ?

Any help appreciated!

Amit Verma
  • 2,450
  • 2
  • 8
  • 21
sanjay
  • 3
  • 2

1 Answers1

1

For JSON manipulation, Json.NET may fit your needs.

It's support LINQ to JSON. You can retrieve the BTC info with below statements.

    var btcLastTradedPrice = JObject.Parse(json).SelectToken("BTC.last_traded_price");
    Console.WriteLine(btcLastTradedPrice);

// Result: 3699003.04

The complete code for this sample:

using System;
using Newtonsoft.Json.Linq;
                    
public class Program
{
    
    public static void Main()
    {
        var json = @"{
    ""BTC"": {
        ""highest_buy_bid"": 3699003.04,
        ""lowest_sell_bid"": 3703660.94,
        ""last_traded_price"": 3699003.04,
        ""yes_price"": 3597618.7,
        ""volume"": {
            ""max"": ""3750000.00"",
            ""min"": ""3577155.82"",
            ""volume"": 43.12889007
        }
    },
    ""XRP"": {
        ""highest_buy_bid"": 95.2,
        ""lowest_sell_bid"": 95.3,
        ""last_traded_price"": 95.2,
        ""yes_price"": 95,
        ""volume"": {
            ""max"": ""97.91"",
            ""min"": ""93.20"",
            ""volume"": 329050.21
        }
    },
    ""NEO"": {
        ""highest_buy_bid"": 4241.3,
        ""lowest_sell_bid"": 4273.03,
        ""last_traded_price"": 4244.85,
        ""yes_price"": 4223.47,
        ""volume"": {
            ""max"": ""4296.35"",
            ""min"": ""4178.65"",
            ""volume"": 3988.2717
        }
    },
}";
        var btcLastTradedPrice = JObject.Parse(json).SelectToken("BTC.last_traded_price");
        Console.WriteLine(btcLastTradedPrice);
    }
}
Le Vu
  • 407
  • 3
  • 12