-1

Problem

Hi, help me please to deserialize Json into a C# dictionary, where key of KeyValuePair = value of 'symbol', and 'cur' i don't want to deserialize.

My JSON

[
  {
    "cur": "BNT",
    "symbol": "BNT/BTC",
    "last": 0.00069999,
    "high": 0.000725,
    "low": 0.000631,
    "volume": 11.83176914,
    "vwap": 0.00066075,
    "max_bid": 0.000725,
    "min_ask": 0.000631,
    "best_bid": 0.00062001,
    "best_ask": 0.0007
  },
  {
    "cur": "FST",
    "symbol": "FST/BTC",
    "last": 0.00000113,
    "high": 0.00000136,
    "low": 0.0000011,
    "volume": 105727.64821274,
    "vwap": 0.00000115,
    "max_bid": 0.0000012,
    "min_ask": 0.00000108,
    "best_bid": 0.00000115,
    "best_ask": 0.00000127
  }
]

C#

public class SomeClass
{
    Dictionary<string,Ticker> dictionary { get; set; }
}

public class Ticker
{
    public decimal Last { get; set; }

    public decimal High { get; set; }

    public decimal Low { get; set; }

    public decimal Volume { get; set; }

    public decimal Vwap { get; set; }

    public decimal MaxBid { get; set; }

    public decimal MinAsk { get; set; }

    public decimal BestBid { get; set; }

    public decimal BestAsk { get; set; }
}

I think I need to use CustomCreationConverter, but i don't know how to do it :)

Thanks

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
N. Kit
  • 5
  • 1
  • 1
    You may not need a custom converter. You do need to read [ask], take the [tour], do more research and ask clearer question – Ňɏssa Pøngjǣrdenlarp Feb 04 '18 at 22:52
  • 1
    See: https://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net Dastun's answer in this thread is what you need. – TomB Feb 04 '18 at 22:59
  • Possible duplicate of [How can I deserialize JSON to a simple Dictionary in ASP.NET?](https://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net) – SherylHohman Feb 05 '18 at 01:00

1 Answers1

0

Your json is an array/list. Deserialize to

List<Ticker>

not to a dictionary.

A more correct model would be

public class Ticker
{
    public string cur { get; set; }
    public string symbol { get; set; }
    public double last { get; set; }
    public double high { get; set; }
    public double low { get; set; }
    public double volume { get; set; }
    public double vwap { get; set; }
    public double max_bid { get; set; }
    public double min_ask { get; set; }
    public double best_bid { get; set; }
    public double best_ask { get; set; }
}

var list = JsonConvert.DeserializeObject<List<Ticker>>(jsonstring);

If you really need a dictionary

var dict = list.ToDictionary(x => x.symbol, x => x);
Eser
  • 12,346
  • 1
  • 22
  • 32
  • I think that `symbol` is meant to be the key else they wont know what the data represents...but who knows, its so very poorly asked. – Ňɏssa Pøngjǣrdenlarp Feb 04 '18 at 22:53
  • No, i need to deserialize it in the dictionary. Then I can extract the required pair very easily, for example: Ticker ticker = dict["BTC/USD]; – N. Kit Feb 04 '18 at 22:59
  • 1
    @N.Kit I also included it in my answer. Read it again (of course it can be done with some json.net tricks like *JsonConverter* but a 2 lines solution is much easier to understand and maintain) – Eser Feb 04 '18 at 23:05
  • @Eser sorry, i didn't see :) – N. Kit Feb 04 '18 at 23:07