Application of JSON in ASP.

Source: Internet
Author: User

first, the ASP back-end returns JSON data, the foreground JS parsing

Read the data in the background and manually encapsulate it in JSON format:

         PublicContentresult Getusersbyorgid (intId) {DataTable table=Userslogic.getusersbyorgid (Id); StringBuilder Data=NewStringBuilder (); foreach(DataRow rowinchtable. Rows) {data. Append (",{"); Data. Append ("\ "userid\": \ ""+ row["UserId"] +"\","); Data. Append ("\ "username\": \ ""+ row["UserName"] +"\""); Data. Append ("}"); }            if(Data. Length >0) Data= data. Remove (0,1); stringresult ="{\ "items\": ["+ convert.tostring (data) +"]}"; returnContent (Result,"Application/json"); }

In the foreground with JS parsing, and display to the page:

function Getorgusers (Id) {$.post ("/stockmanage/ware/getusersbyorgid", {Id:id}, function (data) {varRjson = Json.parse (json.stringify (data));//Convert to JOSN data            if(Rjson! =""&& Rjson! =NULL&& Rjson! ="undefined") {                varHTMLData ="";  for(M =0; M < rJson.items.length; m++) {                    varUID = rjson.items[m]["UserId"]; varuname = rjson.items[m]["UserName"]; HTMLData+="<option value= '"+ UID +"' >"+ uname +"</options>"; }                $("#Select"). HTML (htmldata); }            return;    }); }
Second, JS encapsulated JSON data, passed to the background parsing

JS encapsulated JSON array:

vararr = []; varJsonobj = {}; Jsonobj.userid= 2; Jsonobj.username= "Hellow world!"; Jsonobj.phone= "10086100101";Arr.push (jsonobj); $.post ('/userarea/user/savedata', {"param": Json.stringify (arr)}, function (data) {Success (data); });

Asp. NET background parsing:

        #regionController receives data PublicJsonresult SaveData (stringparam) {

Try { userlogic.addconstruct (param); returnJson (New{Result ="OK", Message ="Operation succeeded" }); } Catch(Exception ex) {returnJson (New{Result ="ERROR", Message =Ex. Message}); } } #endregion #regionSaving data to a database Public Static BOOLAddconstruct (stringJson) { BOOLStatus =false; Try{Jarray Jobject=(Jarray) jsonconvert.deserializeobject (JSON); for(inti =0; i < Jobject.count; i++) {User cs=New User(); varTmpobj =(Jobject) jobject[i]; Cs. UserId= Convert.int (tmpobj["UserId"]); Cs. UserName= tmpobj["UserName"]. ToString (); Cs. Phone= tmpobj["Phone"]. ToString ();//inserting data into the primary tableUserContext.Instance.Insert (CS); } Status=true; } Catch{Status=false; } returnstatus; } #endregion

Application of JSON in ASP.

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.