Jsonresult Serialize and save the JSON file and read the reverse sequence to the JSON file

Source: Internet
Author: User

In the project we often encounter some frequently accessed interfaces, and update timeliness is not particularly high, then we can use the file to do some data request cache.

Here, take the public number of fan user list For example, we put the public number of users to slow the existence of the file, so that the page will not need to re-request the interface.

Get Fan Users

/// <summary>        ///User List/// </summary>        /// <returns></returns>         PublicActionResult userlist () {//when the page loads, the user is pulled to cacheList<wx_user_info> Users =NewList<wx_user_info>(); varOpenlist =NewWeChat ().            Getopenidlist (); if(openlist.responsestate) {foreach(varIteminchOpenList.ResponseData.data.openid) {varBackdata =NewWeChat ().                    GetUserInfo (item); if(Backdata. ResponseState) {Users.add (backdata.                    ResponseData); }                }                varJser =NewJavaScriptSerializer (); varJSON =Jser.                Serialize (Users.toarray ()); stringSrespons =JSON.                ToString (); byte[] Byt =System.Text.Encoding.UTF8.GetBytes (srespons); FileStream FS=NewFileStream (Server.MapPath ("~/app_data/config/weixin.json"), FileMode.OpenOrCreate); Fs. Write (Byt,0, Byt.                Length); Fs.                Dispose (); Fs.            Close (); }            Else{Viewbag.tip="System exception, number:"+ OpenList.ErrorData.errcode +"error message:"+OpenList.ErrorData.errmsg; }            returnView (); }

Asynchronously gets the specified number of bar data from the JSON file when the user pages the page

 PublicActionResult Getuserlist (int? Pageint?Take ) {Page= page??1; Take= take??5; List<wx_user_info> Users =NewList<wx_user_info>(); FileStream FS=NewFileStream (Server.MapPath ("~/app_data/config/weixin.json"), FileMode.Open); byte[] Byt =New byte[FS.                Length]; Fs. Read (Byt,0, Byt.                Length); Users= jsonconvert.deserializeobject<list<wx_user_info>>(Encoding.UTF8.GetString (byt)); Fs.                Dispose (); Fs.                Close (); Viewbag.tip= Users.count () +"people"; Viewbag.totalpage= Math.ceiling ((Double) Users.count ()/(Double)) ; Viewbag.currentpage=page; varPageuseras = Users.where (UA =true); //Conditional Query                if(!string. Isnullorwhitespace (request.form["Search_nickname"]) {Pageuseras= Users.where (ua = Ua.nickname.Contains (request.form["Search_nickname"]) || Ua.remark.Contains (request.form["Search_nickname"])); Viewbag.totalpage= Math.ceiling ((Double) Pageuseras.count ()/(Double)) ; Viewbag.currentpage= page =1; }            //If there is no data on this page, just show the previous page            if(Page >1&& Pageuseras.skip (take. Value * (page. Value-1)). Take. Value). Count () <1) {Viewbag.currentpage= Math.ceiling ((Double) Pageuseras.count ()/(Double)) ; Pageuseras= Pageuseras.skip (take. Value * (page. Value-2)). Take.            Value); }            Else{Pageuseras= Pageuseras.skip (take. Value * (page. Value-1)). Take.            Value); }            returnView (Pageuseras); }

Jsonresult Serialize and save the JSON file and read the reverse sequence to the JSON file

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.