-1

I'm trying to access specific fields of a json string returned when I authenticate to an API. When I attempt to print or manipulate the string I receive

System.NullReferenceException: 'Object reference not set to an instance of an object.'

var response = client.GetAsync(fullUri).Result;
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine("StatusCode : " + (int)response.StatusCode + " " + response.StatusCode.ToString());

var jo = JObject.Parse(content);
var token = jo["Result"]["Token"].ToString(); //error mentioned above received here
Console.WriteLine(token.ToString());          //error mentioned above also recieved here
Console.WriteLine(content);                   //error mentioned above received here too

the string "content" holds the json looking like this:

{
    "Result":{
        "TenantId":"1",
        "Token":"38255507",
        "UserName":"kadmin",
        "RoleId":2,
        "ScopeId":"2",
        "AdminId":16596942,
        "MachineId":null,
        "MachineGroupName":null,
        "CultureInfo":"en-US",
        "TimePref":"Browser",
        "OffSetInMinutes":420,
        "Attributes":null
    },
    "ResponseCode":0,
    "Status":"OK",
    "Error":"None"
}

I've tried applying many examples I've seen on stackoverflow but none seem to work

Teragon
  • 239
  • 3
  • 13

2 Answers2

1

The key is case sensitive. All you need to do is change

var token = jo["result"]["Token"].ToString();

to

var token = jo["Result"]["Token"].ToString();
msitt
  • 1,237
  • 12
  • 27
  • Didn't catch that however I'm still receiving the error. `Console.WriteLine(token);` says System.NullReferenceException: 'Object reference not set to an instance of an object.' – Teragon Mar 31 '17 at 20:32
0

The line should read

var token = jo["Result"]["Token"].ToString();
James Ralston
  • 1,170
  • 8
  • 11