C # Implementation of the Chinese Weather Network JSON interface test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.