0

I am sending two different JSON array as response from servlet to jquery(ajax call).

Say jsonarray1

["E1134","2015-08-22 00:00:00","SK5012","2015-08-21 00:00:00","1621122","2015-08-01 00:00:00","2"] 

Need to pass these to some text fields like E1134 -- Empid text field etc.,

jsonarray2

[{"slno":"1","itemname":"MCB DOUBLE POLE 10A -LEGRAND","itemcode":"2102MCBAC2P10A04","supplier":"SREE KUMAR AGENCIES","receivedqty":"200","rejectedqty":"0","acceptedqty":"200"},{"slno":"2","itemname":"MCB DOUBLE POLE 10A DC -LEGRAND","itemcode":"2102MCBDC2P10A04","supplier":"SREE KUMAR AGENCIES","receivedqty":"106","rejectedqty":"0","acceptedqty":"106"}]

These response will be passed to table like

$.each(responseJson, function(key,value) {
	   				  var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
	            	  	   rowNew.children().eq(0).html('<img src="idata.gif" height="42" width="42" alt="idata" class="popup-trigger">');
	            	       rowNew.children().eq(1).append(i);
	            	  	   rowNew.children().eq(2).text(value['itemname']); 
	                       rowNew.children().eq(3).text(value['itemcode']); 
	                       rowNew.children().eq(4).text(value['receivedqty']); 
	                       rowNew.children().eq(5).text(value['rejectedqty']); 
	                       rowNew.children().eq(6).text(value['acceptedqty']); 
	                       rowNew.children().eq(7).html('<input type="text" class="tb1"/>');
	                       rowNew.appendTo(table1);
	                       i++;
	               });       

jsonarray1 needs to be populated in some text fields. jsonarray2 needs to be populated in table

I am struck with how to get those separately so that I can pass to the required fields. For example response1 for text fields and response2 for tables. Please someone help me on this

try{
			Connection con=QCConProvider.getConnection();
			PreparedStatement ps=con.prepareStatement("query1");
			ResultSet rs=ps.executeQuery();
			
			if (!rs.next() ) {
				System.out.println("no data");
			}else {
				existqcmaster = true;
				irepno = rs.getString("valu");
				cdate = rs.getString("cdate");
				vinvno = rs.getString("vinvno");
				vinvdt = rs.getString("vinvdt");
				pono = rs.getString("pono");
				podt = rs.getString("podt");
				unit = rs.getString("unit");
			}
			
            //close all connection here.
			
		}catch(Exception e){
			System.out.println(e);
		}
			    
			  JSONArray masterdata = new JSONArray();//o/p1 - should send back to ajax
			  masterdata.put(irepno);
			  //pass all values to masterdata
			  System.out.println(masterdata);
			  System.out.println(masterdata.length());			  
			  
			  if (existqcmaster) {
				  System.out.println("qc details available");
				  ArrayList<Elements> inspdata = new ArrayList<Elements>();
				  inspdata=Fetchinsp.getElements(irepno);				  
				  Gson gson = new Gson();
				  JsonElement element = gson.toJsonTree(inspdata, new TypeToken<List<Elements>>() {}.getType());
				  JsonArray jsonArray = element.getAsJsonArray();
				  response.getWriter().print(jsonArray);	// o/p2 - should send back to ajax
			  }
			  response.setContentType("application/json");
			//  response.getWriter().print(masterdata);	
	}
}

6
  • Firstly, note that what you have are arrays. They are nothing to do with JSON at all. Secondly, please give details about the code you've written so far and also the HTML structure you're trying to create. It seems that all you need is a loop (or two) Commented Mar 15, 2017 at 11:05
  • Can you post the code which is handling your ajax calls. And explain what you want to do with the return values, that's not really clear from your question imo. Commented Mar 15, 2017 at 11:07
  • so you're not able to do two separate calls for each array? Commented Mar 15, 2017 at 11:07
  • @LuudJacobs: edited my code. Please check it Commented Mar 15, 2017 at 11:46
  • @AlanSutherland: no: in one call i am trying to get all required values.Please see my code Commented Mar 15, 2017 at 11:59

1 Answer 1

0

To create array from json:

var arrayOfJSon = $.parseJSON(jsonarray1);

To Populate textField by some string value:

$(".SomeTextFieldsClass").val = arrayOfJSon[0];

To create table populated by jSon:
How to put JSON data in html / javascript grid table

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.