Illep
Illep

Reputation: 16851

Error while Deserializing JSON output

I am getting the following error at dynamic jsonText = JsonConvert.DeserializeObject(json);

ERROR

Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

CODE

string api = "https://api.linkedin.com/v1/people/~:(id,first-name,formatted-name,email-address)";
using (var webClient = new WebClient())
{
    webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
    var json = webClient.DownloadString(api );
    dynamic jsonText = JsonConvert.DeserializeObject(json);

}

Upvotes: 0

Views: 1131

Answers (1)

FRL
FRL

Reputation: 776

I think it is necessary to specify that you want the result in json, otherwise some web services returns the data in xml

webClient.Headers.Add(System.Net.HttpRequestHeader.Accept, "application/json");

//also the encoding if need
webClient.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding, "utf-8");

but in linkedin you must use

webClient.Headers.Add("x-li-format", "json");

More info here

https://developer.linkedin.com/docs/rest-api

Upvotes: 2

Related Questions