After an article, after repeated review, and finally found a usable JSON interface from this article, so study the JSON interface test, and the previous XML interface test principle is the same, just need to install Newtonsoft.json:
This is the legendary json.net!. In the project right click on the "Manage NuGet package" Search json.net and then install, wait until the project reference to appear in this thing can be in the program using Newtonsoft.json.
Or old routine, whether useful or useless, first save the JSON content returned by the interface to a local copy. I printed it directly on the console for easy viewing, so I could see the structure of the JSON and analyze it:
By json.net The string is deserialized (can also be strong), and then to analyze the content, as for the test, is generally the comparison value. Here do a traversal, do not compare. The code is as follows:
usingSystem;usingSystem.Text;usingNewtonsoft.json;usingSystem.Net;usingSystem.IO;usingNewtonsoft.Json.Linq;namespacejsoninterfacetest{classProgram {Static voidMain (string[] args) {Test (101010100); } Private Static voidTestintInterfacenumber) { stringURL ="http://www.weather.com.cn/adat/cityinfo/"+ Interfacenumber +". html"; stringLocalcontent = Environment.getfolderpath (Environment.SpecialFolder.DesktopDirectory) +@"\test.txt"; Try{WebClient mywebclient=NewWebClient (); Byte[] Pagedata=mywebclient.downloaddata (URL); stringpagehtml =Encoding.UTF8.GetString (pagedata); using(StreamWriter SW =NewStreamWriter (localcontent)) {SW. WriteLine (pagehtml); } //jobject jobj = Jobject.parse (pagehtml);Jobject jobj = Jsonconvert.deserializeobject (pagehtml) asJobject; Console.WriteLine (Jobj.tostring ()); Readjson (Jobj); Console.ReadLine (); } Catch(WebException webEx) {Console.WriteLine (webEx.Message.ToString ()); } } Private Static voidReadjson (Jobject jobj) {foreach(varOinchjobj) {Console.Write (O.key+":"); if(O.value isjobject) {Console.WriteLine (); Readjson (Jsonconvert.deserializeobject (o.value.tostring ()) asjobject); } Else{Console.WriteLine (o.value); } } } }}
C # Implementation of the Chinese Weather Network JSON interface test