0

i am trying to access the values from ETH_MOD etc. inside a foreach to output the name like ETH_MOD and the last value. the problem is that the keys like ETH_MOD and ETH_LTG are dynamic. They change from time to time and also more new pairs are added.

hope somebody understand my problem and can help ;)

in php i am doing it like this. Here i have the "Name" in $key

$cls = json_decode($json);
foreach ($cls as $key => $value) {  
    $last= transformNumber($value->last);
    $bid= transformNumber($value->bid);
    $ask= transformNumber($value->ask);
}

kind regards

{
  "returnTicker": {
    "ETH_MOD": {
      "tokenAddr": "0x957c30ab0426e0c93cd8241e2c60392d08c6ac8e",
      "quoteVolume": 401812,
      "baseVolume": 2398.702,
      "last": 0.005690802,
      "percentChange": 0.0737,
      "bid": 0.005650001,
      "ask": 0.00579
    },
    "ETH_LTG": {
      "tokenAddr": "0x0879e0c9822b75f31f0b0ed2a30be9f484a57c2f",
      "quoteVolume": 35336143,
      "baseVolume": 1767.741,
      "last": 4.9993E-05,
      "percentChange": 3.1661,
      "bid": 4.65E-05,
      "ask": 4.874E-05
    },
    "ETH_ATM": {
      "tokenAddr": "0x9b11efcaaa1890f6ee52c6bb7cf8153ac5d74139",
      "quoteVolume": 9114955.042,
      "baseVolume": 1641.045,
      "last": 0.000177983,
      "percentChange": -0.0167,
      "bid": 8.0009E-05,
      "ask": 8.38E-05
    },
    "ETH_VERI": {
      "tokenAddr": "0x8f3470a7388c05ee4e7af3d01d8c722b0ff52374",
      "quoteVolume": 3632.34,
      "baseVolume": 617.863,
      "last": 0.164501,
      "percentChange": -0.015,
      "bid": 0.168,
      "ask": 0.1695
    },
    "ETH_ITS": {
      "tokenAddr": "0xfd784da5c740c617aafb80399fa81b86e1da99a5",
      "quoteVolume": 206921.3,
      "baseVolume": 124.563,
      "last": 0.000506,
      "percentChange": 0.3316,
      "bid": 0.0002667,
      "ask": 0.0005588
    }
  },
  "trades": [],
  "orders": {
    "buys": [],
    "sells": []
  }
}
nowkin
  • 25
  • 9

2 Answers2

3

This will give you a hint (i placed your JSON in rawJson string variable):

using Newtonsoft.Json.Linq;//use Nuget to get Newtonsoft package

    ...
    ...

   dynamic task = JObject.Parse(rawJson);
   var ticker = task.returnTicker;
   foreach (var pair in ticker)
   {              
     Console.WriteLine(pair.Name); //ETH_MOD and others 
     Console.WriteLine(pair.Value);//JSONs with details
    }
Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
0

You can mark returnTicker as:

[JsonProperty]
private IDictionary<string, JToken> returnTicker;

If you want to get underlying data from it you may cast it to required object like this:

foreach (var pair in returnTicker) {
  if ("ETH_MOD" == pair.Key) {
  MyEHT_MOD_class object = pair.Value.ToObject<MyEHT_MOD_class>();
  ... // use object
  }
  ...
}

Then just use MyEHT_MOD_class object that contains fields that you need.

If there are may be extra elements in your object and you do not want to miss it you can mark some fild as:

[JsonExtensionData]
private IDictionary<string, JToken> extraStuff;

In general, declare as JsonProperty and use that fields that you need. If there are some new fields that you are not use then it's okay, you may check for _extraStuff and inform user.