Clayton C
Clayton C

Reputation: 551

"Cannot perform runtime binding on a null reference" from JsonConvert.DeserializeObject<dynamic>( response )

Within my code I am receiving the error

"Cannot perform runtime binding on a null reference"

Please note that response is coming from a server API, sample of data attached.

I have split the code on purpose as I have a dedicated method that will output the dynamic object but for testing purposes I have avoiding that.

var response = NetworkHelper.GET( url );
var result = JsonConvert.DeserializeObject<dynamic>( response );

The error is being thrown on the following line of code

if (result.UniqueIdentifier != null && result.UniqueIdentifier != 0)

Sample data

{
    "UniqueIdentifier":8529685323871177582,
    "Id":{},
    "IsLAN":false,
    "Language":"English"
}

Any help will be greatly appreciated.

Upvotes: 1

Views: 2627

Answers (1)

EylM
EylM

Reputation: 6103

Your issue is that the JSON is escaped and your result variable is basically a string type.

You need to properly unecape it, and then deserialize it.

var token = JToken.Parse(response );
var result = JsonConvert.DeserializeObject<dynamic>(token.ToString());

if(result.UniqueIdentifier=!0)
{
  // do something
}

Upvotes: 2

Related Questions