简体   繁体   中英

JavaScriptSerializer Deserialize c#?

Error: The type arguments for method 'System.Web.Script.Serialization.JavaScriptSerializer.Deserialize<T>(string)' cannot be inferred from the usage. 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

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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