反序列化JSON输出时出错

时间:2016-12-25 01:10:27

标签: c# json deserialization linkedin-api

我在dynamic jsonText = JsonConvert.DeserializeObject(json);

收到以下错误消息

ERROR

  

解析值时遇到意外的字符:<。路径'',行   0,位置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);

}

1 个答案:

答案 0 :(得分:2)

我认为有必要在json中指定你想要的结果,否则一些web服务会返回xml中的数据

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

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

但在linkedin中你必须使用

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

此处有更多信息

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