1

I'm trying to deserialize a kind of json as below. The problem is keys are dynamic. But we can find them using instrument_id or product_id. The second problem is about my algorithm. I use a library to get and deserialize string. I only specify api url and library gives it to me as an object. So I have limited options.

  • I can use a custom json converter for class
  • Or I can use regex on JsonProperty Name

But I dont know how. Any Ideas?

[
    {
        "currency:BTC":{
            "available":"0.09995502",
            "leverage":"5",
            "leverage_ratio":"5",
            "rate":"0.00009984"
        },
        "currency:USDT":{
            "available":"400.00000000",
            "leverage":"5",
            "leverage_ratio":"5",
            "rate":"0.00019992"
        },
        "instrument_id":"BTC-USDT",
        "product_id":"BTC-USDT"
    },
    {
        "currency:EOS":{
            "available":"1.9343",
            "leverage":"5",
            "leverage_ratio":"5",
            "rate":"0.00009984"
        },
        "currency:USDT":{
            "available":"7.1571",
            "leverage":"5",
            "leverage_ratio":"5",
            "rate":"0.00019992"
        },
        "instrument_id":"EOS-USDT",
        "product_id":"EOS-USDT"
    }
]
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • You could deserialize the dynamically keyed `"currency:BTC"` values into a `Dictionary` for some appropriate `T` by using `[JsonTypedExtensionData]` from [this answer](https://stackoverflow.com/a/40094403/3744182) to [How to deserialize a child object with dynamic (numeric) key names?](https://stackoverflow.com/q/40088941/3744182). In fact I think this is a duplicate, agree? Or do you need more specific help in applying that answer? – dbc Dec 13 '20 at 15:05
  • I tried JsonTypedExtensionData. But the problem is there are two dynamic keys – Burak Öner Dec 13 '20 at 19:16
  • Seems like `[JsonTypedExtensionData]` works, see https://dotnetfiddle.net/NzQ326. Or do you just not want to use a `Dictionary` for the specific currencies? If not, can you [edit] your question to show your desired data model? – dbc Dec 13 '20 at 19:25
  • 1
    TypedExtensionDataConverter on https://stackoverflow.com/questions/40088941/how-to-deserialize-a-child-object-with-dynamic-numeric-key-names link worked for me. Thank you very much – Burak Öner Dec 13 '20 at 20:04

0 Answers0