To save the JSON data in a static script file read

Source: Internet
Author: User

Some commonly used data such as some of the site's regional information is not likely to be changed, generally not through the request, so we choose to exist in a static file, such as the following demo:

1. Dynamically loading JSON data display to foreground
[HttpPost] PublicActionResult GetData () {List<Area> arealist =NewList<area>()            {                NewArea () {Areaid=1, AreaName="Fujian province", Depth=1, CityList=NewList<area>()                       {                            NewArea () {areaid=2, areaname="Fuzhou", depth=2},                            NewArea () {areaid=2, areaname="Xiamen", depth=2}                       }                },                NewArea () {Areaid=1, AreaName="Sichuan Province", Depth=1, CityList=NewList<area>()                       {                            NewArea () {areaid=2, areaname="Chengdu City", depth=2}                                                }                }            }; return  This. Write (request["Callback"],"",New{success =true, Returndata =arealist}); }
        /// <summary>        ///Conversion Output/// </summary>        /// <param name= "CallBack" ></param>        /// <param name= "paramname" ></param>        /// <param name= "obj" ></param>        /// <returns></returns>         PublicContentresult Write (stringCallBack,stringParamName,Objecto) {Newtonsoft.Json.JsonSerializerSettings jsetting=NewNewtonsoft.Json.JsonSerializerSettings (); Jsetting.nullvaluehandling=Nullvaluehandling.ignore; Jsetting.dateformatstring="YYYY-MM-DD HH:mm:ss"; if(CallBack = =""&& ParamName = ="")                returnContent (Jsonconvert.serializeobject (o, jsetting),"Application/json"); Else if(CallBack! ="")                returnContent (CallBack +"("+ Jsonconvert.serializeobject (o, jsetting) +");","Application/x-javascript"); Else                returnContent ("var"+ ParamName +" = "+ Jsonconvert.serializeobject (o, jsetting) +";","Application/x-javascript"); }
<id= "main">    <id= " Ulmain ">            </ul></Div >
$.ajax ({URL:'/home/getdata ', DataType:' Jsonp ', type:' Post ', Success:function(r) {varhtml = ' ';  for(vari = 0; i < r.returndata.length; i++) {                    varitem =R.returndata[i]; HTML+ = ' <li> ' + item. AreaName + ' </li> 'varHC = "; if(Item.cityList.length > 0) {HC+ = ' <ul> ';  for(varj = 0; J < Item.cityList.length; J + +) {                            varCity =Item.citylist[j]; HC+ = ' <li> ' + City. AreaName + ' </li> '} HC+ = ' </ul> '; } HTML+=HC; }                $("#ulmain"). HTML (HTML); }          });

The results are as follows:

2. Generating static files
      /// <summary>        ///generate static JS file/// </summary>        /// <returns></returns>         PublicActionResult CreateFile () {List<Area> arealist =NewList<area>()            {                NewArea () {Areaid=1, AreaName="Fujian province", Depth=1, CityList=NewList<area>()                       {                            NewArea () {areaid=2, areaname="Fuzhou", depth=2},                            NewArea () {areaid=2, areaname="Xiamen", depth=2}                       }                },                NewArea () {Areaid=1, AreaName="Sichuan Province", Depth=1, CityList=NewList<area>()                       {                            NewArea () {areaid=2, areaname="Chengdu City", depth=2}                                                }                }            }; Newtonsoft.Json.JsonSerializerSettings jsetting=NewNewtonsoft.Json.JsonSerializerSettings (); stringBODY ="var citylist ="+ Newtonsoft.Json.JsonConvert.SerializeObject (arealist, jsetting) +";"; System.IO.File.WriteAllText (Server.MapPath ("~/scripts/temp.js"), body, System.Text.Encoding.UTF8); returnJson (New{success =true, msg ="Build succeeded"}, Jsonrequestbehavior.allowget); }   
<id= "btncreate">     generate static js file </ Button >
    $ ("#btnCreate"). Click (function  () {            $.get (function ( r) {                if (r.success)                {                    alert ("build succeeded");});}        );

Static files in ~/scripts/temp.js:

The contents are as follows:

3. Comment out the AJAX request, point to the static file to read the data

Don't forget to add: <script src= "~/scripts/temp.js" ></script>

4. Final effect display

There is no network request, read the static file, depending on the specific circumstances of the operation.

To save the JSON data in a static script file read

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.