0

I have a string which looks like

{"BTC":{"USD":7358.8},"ETH":{"USD":402.78},"LTC":{"USD":125.37},"DASH":{"USD":335.73},"XMR":{"USD":187.2},"NXT":{"USD":0.1272},"ZEC":{"USD":204.82},"XRP":{"USD":0.5296},"WAVES":{"USD":4},"BCH":{"USD":705.74},"EOS":{"USD":5.89},"XLM":{"USD":0.229},"ADA":{"USD":0.1601},"NEO":{"USD":52.03},"IOT":{"USD":1.07}}

What I want to get are the names(e.g : BTC, ETH, etc), and I was thinking of getting the text before " : " and after " " ", and removing whats unnecessary, but I don't really know if it is a good aproach or how to do it. Any help would be appreciated. Thanks !

weaverbeaver
  • 151
  • 2
  • 10

3 Answers3

4

The string seems JSON, so why not just parse it as is?

public class ExchangeRate
{
    public decimal USD { get; set; }
}

static class Program
{
    static void Main(string[] args)
    {
        string s = "{\"BTC\":{\"USD\":7358.8},\"ETH\":{\"USD\":402.78},\"LTC\":{\"USD\":125.37},\"DASH\":{\"USD\":335.73},\"XMR\":{\"USD\":187.2},\"NXT\":{\"USD\":0.1272},\"ZEC\":{\"USD\":204.82},\"XRP\":{\"USD\":0.5296},\"WAVES\":{\"USD\":4},\"BCH\":{\"USD\":705.74},\"EOS\":{\"USD\":5.89},\"XLM\":{\"USD\":0.229},\"ADA\":{\"USD\":0.1601},\"NEO\":{\"USD\":52.03},\"IOT\":{\"USD\":1.07}}";

        var er = JsonConvert.DeserializeObject<Dictionary<string, ExchangeRate>>(s);

        Console.ReadKey();
    }
}

If you expect multiple currencies, you can use:

var er = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, decimal>>>(s);

Related: Easiest way to parse JSON response

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Yeah, it's JSON, but it will contain ~100 entries like that, and i'd have to manually write that s string you have which I think is not very efficient. But thanks a lot ! – weaverbeaver Apr 03 '18 at 14:17
  • 4
    @weaverbeaver thats not how it works, where is your JSON coming from? The bottom line is your JSON is exactly a `Dictionary`, so use the `JsonConvert.DeserializeObject` method like Patrick said and pass it whatever JSON string. If your JSON does not look like your example then you need to provide a [MCVE] – maccettura Apr 03 '18 at 14:20
  • Solved it, thanks a lot and sorry for the newbie question. – weaverbeaver Apr 03 '18 at 14:31
1

I believe you are trying to use ticker API of some crypto-currency exchange to create probably a desktop notification app. I created one few months back and here is what i did:

  1. Converted JSON to C# class using json2csharp.com website. Name it say JsonClass
  2. Imported Newtonsoft.Json package in my C# project.
  3. Used following code to initialize class

    JsonTextReader reader = new JsonTextReader(new StringReader(JsonString));
    if (reader.Read())
    {
        JsonSerializer serializer = new JsonSerializer();
        jsonClassObject = serializer.Deserialize<jsonClass>(reader);
    }
    
Sandeep
  • 333
  • 2
  • 7
0

From your comment:

What I want to get are the names(e.g : BTC, ETH, etc)

It sounds like you just want the keys of the JSON.

With that in mind you dont even need to care what object the JSON is, just that its a Dictionary of some object. We use a little LINQ to select all the keys and viola:

//Gives an List<string> that is a collection of all the coin names
List<string> coins = JsonConvert.DeserializeObject<Dictionary<string, object>>(someJson)
                .Select(x => x.Key)
                .ToList();

This results in an List<string> that contains this data:

BTC,
ETH,
LTC,
DASH,
XMR,
NXT,
ZEC,
XRP,
WAVES,
BCH,
EOS,
XLM,
ADA,
NEO,
IOT

Fiddle here

maccettura
  • 10,514
  • 3
  • 28
  • 35