Hi how to get json array elements in different ids in response?
//server side jsp
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONArray"%>
<%
JSONArray arrayObj=new JSONArray();
JSONObject obj=new JSONObject();
obj.put("name","data");// how can i display string "name" in secondcombobox id?
obj.put("roll_no","data"); how can i display string "roll_no" in thirdcombobox id?
out.println(obj.toString());
%>
client side jsp where retrieving json array elements in javascript
$("#firstcombo").change(function() {
$.get('comboboxpage.jsp', { combo1Val : $(this).val() }, function(responseData) {
$("#secondcomboboxid").replaceWith(data.name);// here i want to display name, how can i do it?
$("#thirdcomboboxid").replaceWith(data.roll_no);// here i want to display roll no, how can i do it?
});
});
<input type="text" id="secondcomboboxid" name="secondcomboboxid"/>// how can i show name here?
<input type="text" id="thirdcomboboxid" name="thirdcomboboxid"/> how can i show roll_no here?
How can i display name in secondcomboboxid and roll no in thirdcomboboxid? I googled a lot but could not find any solution, Any ideas please
-
post the json you are getting in the response from the serverRafay– Rafay2012年02月09日 06:19:55 +00:00Commented Feb 9, 2012 at 6:19
-
Please post the full string value of responseData so I can be sure of it's format.vinczemarton– vinczemarton2012年02月09日 06:20:19 +00:00Commented Feb 9, 2012 at 6:20
-
@3nigma i am getting no response from server side, how should i modify JSON Array objects to get response in client side?Tom– Tom2012年02月09日 06:23:01 +00:00Commented Feb 9, 2012 at 6:23
-
@SoonDead full string value of responseData means? If i am writing out.println("somevalue"); in server side then i am getting "somevalue" in secondcombobox id and also in thirdcombobox id, which i want to separate, how should i modify my client and server side code?Tom– Tom2012年02月09日 06:25:23 +00:00Commented Feb 9, 2012 at 6:25
-
What does the network tab say in e.g., FireBug ?fncomp– fncomp2012年02月09日 06:27:16 +00:00Commented Feb 9, 2012 at 6:27
4 Answers 4
If you are loading JSON data, you'd better use the method .getJSON() that will take care of parsing the response to a JSON object automatically.
Then you response data being an object, you can access its properties with the dot notation: data.name.
Special case thought for the second property "roll no". As there is a white space, you would have to use the array notation to access the value: data['roll no'].
$.getJSON('comboboxpage.jsp', { combo1Val : $(this).val() }, function(data) {
$("#secondcomboboxid").replaceWith(data.name);
$("#thirdcomboboxid").replaceWith(data['roll_no']);
});
More generally, as mentionned by @diEcho, to parse a json string, you can use $.parseJSON()
7 Comments
data['roll no'] or is there some change caused by arrayObj.add()?['name', 'roll no'] and you'll have to use data[0] and data[1] to access the values.try this
<%
JSONObject arrayObj= new JSONObject();
arrayObj.put("name","john");
arrayObj.put("roll no","007");
response.setContentType("application/json");
response.getWriter().write(arrayObj.toString());
%>
client side
$.ajax({
url:'comboboxpage.jsp',
dataType:'json',
type:'GET',
success:function(data){
console.log(data);
},
error:function(jxhr){
console.log(jxhr.responseText);
}
});
here is a helpful link
2 Comments
$.ajax instead and make a error handler. are you sure the path to comboboxpage.jsp is correct, what does the firebug tells you, does it show status 200 ?So, if I get your setup correctly, you're handling e.g., :8080 with some simple servlet and you want to give back JSON data via a jsp. I think this is not a great idea, but for prototyping, I suppose... Here's a possible just make it work:
<%
JSONArray arrayObj=new JSONArray();
arrayObj.put("name");
arrayObj.put("roll no");
out.println(arrayObj.toString());
%>
See above for the front-end components.