Csharp: using System. Web. Script. Serialization read json

Source: Internet
Author: User

Using System; using System. data; using System. configuration; using System. collections; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; // Add System in the project first. web. extensions reference. net 3.5 using System. web. script. serialization; using System. collections. specialized; using System. collections. generic; using System. text; using System. net; namespace JSONFromCS {public partial class WebForm3: System. web. UI. page {// <summary> /// deserialize json data to Dictionary // </summary> /// <param name = "jsonData"> json data </ param> // <returns> </returns> private Dictionary <string, object> JsonToDictionary (string jsonData) {// instantiate the new instance of the JavaScriptSerializer class JavaScriptSerializer jss = new JavaScriptSerializer (); try {// convert the specified JSON string to a Dictionary <string, object> type object return jss. deserialize <Dictionary <string, object> (jsonData);} catch (Exception ex) {throw new Exception (ex. message );}} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> protected void Page_Load (object sender, eventArgs e) {string url =" http://www.weather.com.cn/data/cityinfo/101280601.html "; WebClient wc = new WebClient (); wc. encoding = System. text. encoding. UTF8; // define the object language string json = wc. downloadString (url); // string json = "{\" weatherinfo \ ": {\" city \ ": \" Shenzhen \ ", \" cityid \": \ "101280601 \", \ "temp \": \ "32 \", \ "WD \": \ "southwest wind \", \ "WS \": \ "Level 4 \", \ "SD \": \ "68% \", \ "WSE \": \ "4 \", \ "time \": \ "16:40 \", \ "isRadar \": \ "1 \", \ "Radar \": \ "JC_RADAR_AZ9755_JB \"}}"; stringBuilder strb = new StringBuilder (); Dictionary <string, object> dic = JsonToDictionary (json); // convert Json data to dictionary format Dictionary <string, object> dataSet = (Dictionary <string, object>) dic ["weatherinfo"]; // use KeyValuePair to traverse data foreach (KeyValuePair <string, object> item in dataSet) {strb. append (item. key + ":" + item. value + "<br/>"); // displayed on the page} Response. write (strb. toString ());}}}

 

Related Article

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.