简体   繁体   English

JavaScriptSerializer 反序列化 c#?

[英]JavaScriptSerializer Deserialize c#?

Error: The type arguments for method 'System.Web.Script.Serialization.JavaScriptSerializer.Deserialize<T>(string)' cannot be inferred from the usage.错误:无法从用法推断方法'System.Web.Script.Serialization.JavaScriptSerializer.Deserialize<T>(string)'的类型参数。 Try specifying the type arguments explicitly.尝试明确指定类型参数。

string url = null;
public int GetLinkedIn()
{
    var jsonString = new System.Net.WebClient().DownloadString("http://www.linkedin.com/countserv/count/share?url=" + url + "&format=json");

    var json = new JavaScriptSerializer().Deserialize<dictionary dynamic="" string="">>(jsonString);
    var count = Convert.ToInt32(json["count"]);

    return count;
}

You can use JavaScriptSerializer class to resolve the Json response您可以使用 JavaScriptSerializer 类来解析 Json 响应

int GetLinkedIn(string url)
{
   url = "http://www.linkedin.com/countserv/count/share?url=" + url + "&format=json";
   var jsonString = new WebClient().DownloadString(url);
   return new JavaScriptSerializer().Deserialize<LinkdInJson>(jsonString).count;
}

And you need to create a class to represent the entity of the Json response并且需要创建一个类来表示Json响应的实体

public class LinkdInJson
{
    public int count { get; set; }
    public string fCnt { get; set; }
    public string fCntPlusOne { get; set; }
    public string url { get; set; }

}

Deserialize方法的类型参数是错误的,正确的形式是

var dic = new JavaScriptSerializer().Deserialize<Dictionary<string, dynamic>>(jsonString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM