如何用VB.net解析Net.WebClient结果

时间:2014-07-29 20:08:34

标签: vb.net webclient

Using client As New Net.WebClient
    Dim reqparm As New Specialized.NameValueCollection
    'reqparm.Add("param1", "somevalue")
    'reqparm.Add("param2", "othervalue")
    Dim responsebytes = client.UploadValues("http://ip2country.sourceforge.net/ip2c.php?format=JSON", "POST", reqparm)
    Dim responsebody = (New Text.UTF8Encoding).GetString(responsebytes)

End Using

结果:

  

{ip:“184.23.135.130”,主机名:   “184-23-135-130.dedicated.static.sonic.net”,COUNTRY_CODE:   “US”,country_name:“United States”}

寻求任何帮助

1 个答案:

答案 0 :(得分:0)

我明白了。我包括了Json.net的参考资料。

 Imports Newtonsoft.Json.Linq
 Imports System.Net

 Using client As New Net.WebClient
        Dim reqparm As New Specialized.NameValueCollection
        'reqparm.Add("param1", "somevalue")
        'reqparm.Add("param2", "othervalue")
        Dim responsebytes = client.UploadValues("http://ip2country.sourceforge.net/ip2c.php?format=JSON", "POST", reqparm)
        Dim responsebody = (New Text.UTF8Encoding).GetString(responsebytes)
        Dim blah As String = client.DownloadString("http://ip2country.sourceforge.net/ip2c.php?format=JSON")
        Dim json As JObject = JObject.Parse(responsebody)
        Console.WriteLine(json.SelectToken("ip"))
        Console.WriteLine(json.SelectToken("hostname"))
        Console.WriteLine(json.SelectToken("country_code"))
        Console.WriteLine(json.SelectToken("country_name"))
        Console.ReadKey()
 End Using