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.