Getting started with JSON

Source: Internet
Author: User

A very small feature was made in March, namely getting data from the database and passing the data to the iPhone client through JSON, I don't know if all iPhone development projects require JSON. All I do is provide a data interaction interface, and a C # MVC project is created under vs2010, I changed the framework of the previous project management system and handed in the work.

I did not know about JSON, but I found it useful after use. It is nothing more than a data transmission format. Generally, list is used for data encapsulation, some code written in the project is listed here:

The VOD function creates a class under the Controller for storage. Some database access code can be ignored.

/// VOD [httpget] public jsonresult VOD (Int? Page) {var page1 = page ?? 0; // assetmodel asset = new assetmodel (); assetdal assetop = new assetdal (); filterasset filter = new filterasset (); filter. type = 0; dataset DS = assetop. query (filter, asset); int Total = Ds. tables [0]. rows. count; filter. page = page1; DS = assetop. query (filter, asset); // type table categorymodel Category = new categorymodel (); categorydal categoryop = new categorydal (); // Program-type table sortmodel sort = new sortmodel (); sortdal sortop = new sortdal (); filtersort filters = new filtersort (); vaR list = new list <VOD> (); var list2 = new list <titleasset> (); For (INT I = 0; I <Ds. tables [0]. rows. count; I ++) {int id = convert. toint32 (Ds. tables [0]. rows [I] ["no."]. tostring (); String title = Ds. tables [0]. rows [I] ["title"]. tostring (); string poster = Ds. tables [0]. rows [I] ["poster address"]. tostring (); string image = Ds. tables [0]. rows [I] ["image address"]. tostring (); int rating = convert. toint32 (Ds. tables [0]. rows [I] ["rating"]. tostring (); filters. assetid = ID; dataset ds2 = sortop. query (filters, sort); string upset = ""; for (int K = 0; k <ds2.tables [0]. rows. count; k ++) {upset + = categoryop. getname (convert. toint32 (ds2.tables [0]. rows [k] ["type number"]. tostring () + ",";} string categorys = upset; var asset1 = new titleasset () {assetid = ID, Title = title, poster = poster, image = image, rating = rating, categories = upset}; list2.add (asset1);} var vod1 = new VOD () {Total = total, assets = list2}; List. add (vod1); Return JSON (list, jsonrequestbehavior. allowget );}

Then there are some data entity classes. Considering nesting and hierarchy, a class also contains various lists.

// The public class titleasset {// data member public int assetid {Get; set;} Public String title {Get; set;} Public String poster {Get; set;} public string image {Get; set;} public int rating {Get; set;} Public String categories {Get; set;} Public String description {Get; set ;}} // group public class group {public string name {Get; set;} public list <titleasset> assets = new list <titleasset> ();} // live broadcast public class live {public int total {Get; set;} public list <titleasset> assets = new list <titleasset> ();} // VOD public class VOD {public int total {Get; set;} public list <titleasset> assets = new list <titleasset> ();} // program details public class detail {public list <titleasset> assets = new list <titleasset> (); public list <metadata> medadatas = new list <metadata> (); public String constent {Get; Set ;}}

In fact, the content of Data encapsulation is just the following lines of code. Data Nesting is to define the list to assign values first, and then include the current list in another list.

VaR list2 = new list <metadata> (); var meta1 = new metadata () {name = ctor, value = actors}; list2.add (meta1 ); // m3u8 file table constentmodel constent = new constentmodel (); constentdal CONOP = new constentdal (); string constents = CONOP. geturl (ID); var list = new list <detail> (); var detail1 = new detail () {assets = list1, medadatas = list2, constent = constents}; List. add (detail1); Return JSON (list, jsonrequestbehavior. allowget );

Enter http: // localhost: 1980/asset in the browser during the test.

The query results are displayed on the page, in JSON format. Chrome does not look very friendly.

[{"Assets": [{"assetid": 1, "title": "Mission Impossible 4", "poster": "123", "image": "123 ", "rating": 1, "categories": "plot, action, adventure,", "Description": NULL },{ "assetid": 2, "title ": "Mission 3", "poster": "123", "image": "123", "rating": 1, "categories": "", "Description ": null}], "name": "foreign film" },{ "assets": [], "name": "Chinese film"}]

PS: vs2010 is really easy to use, especially for the MVC Architecture, where Javascript is automatically embedded. It seems that Microsoft is not arrogant and only supports its own products.

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.