简体   繁体   English

在javascript中获取json数组对象

[英]get json array objects in javascript

Hi how to get json array elements in different ids in response? 嗨,如何获取不同ID中的json数组元素作为响应?

//server side jsp //服务器端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 客户端jsp,在javascript中检索json数组元素

 $("#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 ? 我该如何在secondcomboboxid中显示名称 ,而在Thirdcomboboxid则不 滚动 I googled a lot but could not find any solution, Any ideas please 我在Google上搜索了很多,但找不到任何解决方案,请问任何想法

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. 如果要加载JSON数据,则最好使用.getJSON()方法,该方法将负责自动解析对JSON对象的响应。

Then you response data being an object, you can access its properties with the dot notation: data.name. 然后,您将响应数据作为对象,则可以使用点符号data.name来访问其属性。

Special case thought for the second property "roll no". 考虑第二个属性“ roll no”的特殊情况。 As there is a white space, you would have to use the array notation to access the value: data['roll no']. 由于存在空格,因此必须使用数组符号来访问值: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() 如@diEcho所述,更一般而言,要解析json字符串,可以使用$ .parseJSON()

use 采用

responseData.name
responseDate.roll_no

or use parseJSON 或使用parseJSON

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

So, if I get your setup correctly, you're handling eg, :8080 with some simple servlet and you want to give back JSON data via a jsp. 因此,如果我正确地进行了设置,则说明您正在使用一些简单的servlet处理:8080,而您想通过jsp退回JSON数据。 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. 有关前端组件,请参见上文。

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 这是一个有用的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM