接上一篇,经过反复的查看,最终从这篇文章中找到了一个可用的JSON接口,于是研究了一下中国天气网JSON接口的测试:
和上一篇XML接口测试的原理是一样的,只是需要安装一下Newtonsoft.Json:
这个就是传说中的JSON.Net!在项目右键点击“管理NuGet程序包”中搜索json.net然后安装即可,等到项目的引用中出现这个东西的时候就可以在程序里using Newtonsoft.Json了。
还是老套路,不管有用没用,先把接口返回的JSON内容保存到本地一份。这里为了查看方便我直接在控制台打印出来了,方便查看JSON的结构并加以分析:
通过JSON.Net对字符串进行反序列化(也可以强转),然后对内容加以分析即可,至于想测试,一般是比较值。这里就做一次遍历,不做比较了。代码如下:
using System; using System.Text; using Newtonsoft.Json; using System.Net; using System.IO; using Newtonsoft.Json.Linq; namespace JsonInterfaceTest { class Program { static void Main(string[] args) { test(101010100); } private static void test(int interfaceNumber) { string url = "http://www.weather.com.cn/adat/cityinfo/" + interfaceNumber + ".html"; string localContent = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @" est.txt"; try { WebClient MyWebClient = new WebClient(); Byte[] pageData = MyWebClient.DownloadData(url); string pageHtml = Encoding.UTF8.GetString(pageData); using (StreamWriter sw = new StreamWriter(localContent)) { sw.WriteLine(pageHtml); } //JObject jObj = JObject.Parse(pageHtml); JObject jObj = JsonConvert.DeserializeObject(pageHtml) as JObject; Console.WriteLine(jObj.ToString()); ReadJson(jObj); Console.ReadLine(); } catch (WebException webEx) { Console.WriteLine(webEx.Message.ToString()); } } private static void ReadJson(JObject jObj) { foreach (var o in jObj) { Console.Write(o.Key+":"); if (o.Value is JObject) { Console.WriteLine(); ReadJson(JsonConvert.DeserializeObject(o.Value.ToString()) as JObject); } else { Console.WriteLine(o.Value); } } } } }
换汤不换药,稍微改一下上面的接口测试程序就可以通过快递接口查快递了(通过快递公司代号和快递单号):
using System; using System.Text; using Newtonsoft.Json; using System.Net; using System.IO; using Newtonsoft.Json.Linq; namespace JsonInterfaceTest { class Program { static void Main(string[] args) { test("zhongtong", 368583049476); Console.Read(); } private static void test(string a, long b) { string url = "http://www.kuaidi100.com/query?type=" + a + "&postid=" + b; string localContent = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @" est.txt"; try { WebClient MyWebClient = new WebClient(); Byte[] pageData = MyWebClient.DownloadData(url); string pageHtml = Encoding.UTF8.GetString(pageData); using (StreamWriter sw = new StreamWriter(localContent)) { sw.WriteLine(pageHtml); } JObject jObj = JObject.Parse(pageHtml); //JObject jObj = JsonConvert.DeserializeObject(pageHtml) as JObject; Console.WriteLine(jObj.ToString()); ReadJson(jObj); Console.ReadLine(); } catch (WebException webEx) { Console.WriteLine(webEx.Message.ToString()); } } private static void ReadJson(JObject jObj) { foreach (var o in jObj) { Console.Write(o.Key + ":"); if (o.Value is JObject) { Console.WriteLine(); ReadJson(JsonConvert.DeserializeObject(o.Value.ToString()) as JObject); } else { Console.WriteLine(o.Value); } } } } }
其实用PowerShell脚本来完成这个测试是更加方便的!微软给我们提供了很方便的“ConvertFrom-Json”这一cmdlet来实现我们对Json对象的交互!代码如下:
function test() { param($interfaceNumber) $url = "http://www.weather.com.cn/adat/cityinfo/" + $interfaceNumber + ".html"; $jsonObject = Invoke-WebRequest -Uri $url | %{$_.Content} | ConvertFrom-Json if($jsonObject.weatherinfo.img1 -eq "d1.gif"){Write-Host "Test pass!" -ForegroundColor Green} } test -interfaceNumber 101010100
调用脚本执行效果如下: