ASP. NET outputs JSON format data

Source: Internet
Author: User

Recently, Windows 8 Metro App is being developed using JavaScript and HTML development environments. Therefore, binding operation data uses JSON format data. ASP. NET is used in the background. Because the project is relatively small and no database is used in the background, XML is used to save data. The foreground needs to request the background to return data in JSON format. For example, if an eventslist is returned, the XML method in the background is omitted. This article mainly discusses how to use Hashtable and ArrayList, add the JavaScriptSerializer that comes with ASP. NET to generate JSON data, and then return it to the front-end for use. Of course, Microsoft's ASP. MVC and ASP. NET Web APIs greatly improve the processing of JSON format data. In particular, the front end uses Ajax to post a json data to the server, and the backend deserializes an object, you can easily implement the business logic of the background.

The Code is as follows:

ArrayList eventList = new ArrayList (); for (int I = 0; I <3; I ++) {Hashtable ht = new Hashtable (); ht. add ("eventid", I + 1); ht. add ("eventname", "Christmas"); ht. add ("eventdate", "2012-12-25"); ht. add ("eventlocation", "corporate conference center"); eventList. add (ht);} JavaScriptSerializer ser = new JavaScriptSerializer (); String jsonStr = ser. serialize (eventList); Response. write (jsonStr );
 

View the page and you can see that the returned result is JSON data. It is actually an array, and each item in the array is a json object.

[{"Eventdate": "2012-12-25", "eventlocation": "corporate conference center", "eventname": "Christmas", "eventid": 1 },{ "eventdate ": "2012-12-25", "eventlocation": "Company conference center", "eventname": "Christmas", "eventid": 2 },{ "eventdate": "2012-12-25 ", "eventlocation": "Company conference center", "eventname": "Christmas", "eventid": 3}]

We can use this URL to verify that the generated json is correct. Http://jsonlint.com/

However, we need to note that hashtable is out of order when storing data, so the data is not displayed in the order in which we insert data. The expected data format is:

{"Eventid": 1 "," eventname ":" Christmas ", eventdate": "2012-12-25", "eventlocation": "corporate conference center "}

But we can see that our results are not like this, but after the data is returned to the front-end, we still need to use JSON. the parse () method is converted to a json object, so it does not affect our development in a specific order.

Data is transmitted over HTTP using a second-level stream. Therefore, when data arrives at the client, it must be transmitted through JSON. parse (). My project is a Windows 8 Metro App. Of course, it can also be a webpage or web application.

Of course, if the front-end and back-end data models are consistent, you can also directly serialize A json format data through the object instance.

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.