Summary of combined use of JSON jquery Servlet

Source: Internet
Author: User

JSON is used in the project over the past few days.

Jquery's front-end using JSON with Servlet is basically divided into two modes:

1. Basic Key-value pairs

This is the most common method. jquery Ajax is used in almost every way.

For example:

// Cascade database bit code $ ("# lgort"). Load ("lgort. do? Method = getlgortbybukrs ", {" bukrs ": bukrs });
Corresponding servlet:
String bukrs = request.getParameter("bukrs");

 

$.post("mard.do?method=getMard4Out",{matnr:matnr,lgort:lgort,lfimg:lfimg,curDeliveryNr:curDeliveryNr},function(data){   var html ='';   $.each(data, function(k,v){ html += '<tr>' ;html +=  '<td> <span id="lgort">' + v.lgort+'</span></td>';html +=  '<td> <input type="text" name="lagpcode" id="lagpcode" readonly value="'+v.lagpcode+ '"'+  ' class="input_noteidt3" /></td>';html +=  '<td><span id="mantnr">' + v.matnr+'</span></td>';html +=  '<td> <input type="text" name="batchCode" id="batchCode" readonly value="'+v.batchcode+ '"'+ ' class="input_noteidt3" /></td>';html +=  '<td> <input type="text" name="menge" id="menge" readonly value="'+v.labst+ '"' + ' class="input_noteidt3" /></td>';html +=  '<td> <input type="text" name="meins" id="meins" readonly value="'+v.meins+ '"'     + ' class="input_noteidt3" /></td>';html +=  '<td><span id="budate">' + v.budate+'</span></td>';html +=  '<td>'+'<input type="radio" name="radio" id="radio" value="'+v.mardid+'"/> </td>';html += '</tr>' ;   });$("#messageTab").append(html);},"json");  
Corresponding background code:
// Obtain the passed item number and location string matnr = request. getparameter ("matnr"); string lgort = request. getparameter ("lgort"); string lfimg = request. getparameter ("lfimg ");
Method 2: pass an array:
This method can be divided into two situations:
(1) an array of value types, such as [1] and ['2 ']
// Save row Project Quantity split $ ("# saveselect "). click (function () {If (uncompletecount> 0) {alert ('new rows have not been saved. Please save them first !! '); Return} if ($ ("# messagetab TR: GT (0)"). Length <1) {alert (' no project can be entered! '); Return ;} // create an array to save the Mardid corresponding to the current outgoing delivery order number line project // The First Action current outgoing delivery order number | line item number var mards = new array (); mards. push (curdeliverynr); $ ("# messagetab TR: GT (0 )"). each (function (index, element) {var Mardid = trim ($ (this ). find (": Radio "). val (); mards. push (Mardid) ;}); $. post ("Mard. do? Method = getdatabyjson ", {mards: mards}, function () {}); // close the layer $ (" # mybg "). hide (); $ ("# message_box "). hide (); // Save the number of shards to itemcount + = (mards. length-1); alert ('number of projects in the row has been split! ');});

 

The background code is as follows:

// Here is mards [], not mards
String [] mards = request. getparametervalues ("mards []"); If (mards! = NULL & mards. length> 0) {// The first element is the outgoing delivery order number + line project number string key = mards [0]; List <string> IDs = new arraylist <string> (); for (INT I = 1; I <mards. length; I ++) {IDs. add (mards [I]); // system. out. println (mards [I]);} If (outmardmap = NULL) {outmardmap = new hashmap <string, list <string> ();} // forcibly overwrite the old pairing relationship outmardmap. put (Key, IDS);} else {Throw new exception ("failed to get the Mardid corresponding to the row project! ");}

(2) object array return

Foreground $ ("# messagetab TR: GT (0 )"). each (function (index, element) {var tritem = new object (); var vbeln = $ (this ). find ("# vbeln "). val () var ebelp = $ (this ). find ("# ebelp "). val () var barcode = $ (this ). find ("# barcode "). val (); var matnr = $ (this ). find ("# matnr "). val (); var batchcode = $ (this ). find ("# batchcode "). val (); var menge = $ (this ). find ("# menge "). val (); var Meins = $ (this ). find ("# Meins "). val (); lagpcodevar lagpco De = $ (this ). find ("# lagpcode "). val (); // specify the lgort value for the form on the pop-up layer bukrsformvar lgort = trim ($ ("# lgort" ).html ()); vaR bukrs = trim ($ ("# bukrs" ).html (); tritem. lgort = lgort; tritem. bukrs = bukrs; tritem. vbeln = vbeln; tritem. ebelp = ebelp; tritem. barcode = barcode; tritem. matnr = matnr; tritem. batchcode = batchcode; tritem. menge = menge; tritem. meins = Meins; tritem. lagpcode = lagpcode; tbdata. push (tritem) ;}); // alert (tbdata. leng Th); // alert (tbdata [0] ['barcode']); var jsondata = JSON. stringify (tbdata); alert (jsondata); $. post ('tr. do? Method = save2pocache ', {jsondata: jsondata}, function () {}, 'json ');

Background:

String jsondata = request. getparameter ("jsondata");/** because the foreground is in the form of [{}, {}], it is received using jsonarray. If it is {'users ':[{}, {}]} Then use jsonobject to receive */jsonarray ja = jsonarray. fromobject (jsondata); For (Object O: Ja) {jsonobject Jo = (jsonobject) O; If (ebelp = "") {ebelp = Jo. getstring ("ebelp");} If (vbeln = "") {vbeln = Jo. getstring ("vbeln");} tritem item = new tritem (); item. settriid (UUID. generatepk (); // do not set trhiditem. settrhid (""); item. setbarcode (Jo. getstring ("barcode"); item. setmatnr (Jo. getstring ("matnr"); item. setbukrs (Jo. getstring ("bukrs"); item. setlgort (Jo. getstring ("lgort"); item. setlagpcode (Jo. getstring ("lagpcode"); item. settypeid ("lgtype001"); item. setbatchcode (Jo. getstring ("batchcode"); item. setmenge (Jo. getstring ("menge"); item. setmeins (Jo. getstring ("Meins"); item. setshkzg (""); item. setdelflag (""); item. settranstate (""); item. setcurlineno (ebelp); trems. add (item );}

<

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.