1
votes

I am trying to read Rest Web API response. I am getting error while parsing the data

Response Content type : application/json; charset=utf-8

Response: {"access_token":"ot-a4943ac25cf94df3adacd11c71b8ea01","token_type":"Bearer","scope":["READ_USER_PROFILE","WRITE_CONVERSATIONS","READ_CONVERSATIONS"]}

DTO:

public class ResponseData
    {
        public string AccessToken { get; set; }
        public string TokenType { get; set; }
        public string Scope { get; set; }
    }

public class Scope
    {
        public string[] RequestScope { get; set; }
    }

Code:

using (HttpContent rescontent = response.Result.Content)
            {
                // ... Read the string.
                Task<string> result = rescontent.ReadAsStringAsync();
               // res = result.Result;

                var responseData = JsonConvert.DeserializeObject<ResponseData>(result.Result);

                Console.WriteLine("Response: {0}", responseData.AccessToken);
            }

Error: Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: [. Path 'scope', line 1, position 85.'

Programming Language : C#

Please let me know, how to correct this? I want to read the access token parameter from the response.

1

1 Answers

1
votes

You are missing the array denoted by [ and ] in the json

{ 
   "access_token":"ot-a4943ac25cf94df3adacd11c71b8ea01",
   "token_type":"Bearer",
   "scope":[ // this is an array
      "READ_USER_PROFILE",
      "WRITE_CONVERSATIONS",
      "READ_CONVERSATIONS"
   ]
}

You need to modify your class

public List<string> Scope { get; set; }

or

public string[] Scope { get; set; }

Edit

You may also have other errors, so you might need to change to this

public string Access_Token { get; set; }
public string Token_Type { get; set; }

or

[JsonProperty("access_token")]
public string Access_Token { get; set; }

[JsonProperty("token_type")]
public string TokenType { get; set; }