Home > Enterprise >  accessing object json.`params` (reserved keyword) from (dynamic)json in c#
accessing object json.`params` (reserved keyword) from (dynamic)json in c#

Time:05-07

I am calling an api which returns some json. The format of the received json is as follows:

{
  "method": "depth.update",
  "params": [
    true,
    {
      "asks": [
        [
          "8000.00",
          "9.6250"
        ]
      ],
      "bids": [
        [
          "8000.00",
          "9.6250"
        ]
      ]
    },
    "EOS_USDT"
  ],
  "id": null
}

Note the Object paramswith asks and bids, which I am trying to access.

The json is beeing deserialized into a dynamic.

As shown in the example below, I can access the objects by calling json.method for instance.

// retrieve response
string message = Receivestring();
// deserialize
dynamic json = JsonConvert.DeserializeObject<dynamic>(message);
// check if we are in the correct method
if (json.method == "depth.update")
{
    // -> this does not work <--
    dynamic parameters = json.params;
}
else if (json.method == "other.method")
{
    dynamic success = json.result;
}

issue: json.params will not work. I guess it is a reserved keyword, Visual Studio tries to match. How can I access json.params then?

CodePudding user response:

Because params is a keyword for c#, you can try to use @ to escape keyword

json.@params
  • Related