How to send JSON array from server to client, i.e. (java to AJAXJavascript)? - Stack Overflow

I have the following JSON array in my JSON.java file:ArrayList array=new ArrayList();array.add("D

I have the following JSON array in my JSON.java file:

ArrayList array=new ArrayList();
array.add("D");
array.add("A");
array.add("L");

I would like to send array to the AJAX script located in AJAX.jsp. I know how to receive the text in AJAX via e.g.

document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

But I don't know how to send the array from the server to the client! Appreciate your help

I have the following JSON array in my JSON.java file:

ArrayList array=new ArrayList();
array.add("D");
array.add("A");
array.add("L");

I would like to send array to the AJAX script located in AJAX.jsp. I know how to receive the text in AJAX via e.g.

document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

But I don't know how to send the array from the server to the client! Appreciate your help

Share Improve this question edited Aug 16, 2011 at 21:10 DalNM asked Aug 16, 2011 at 19:35 DalNMDalNM 2913 gold badges6 silver badges13 bronze badges 3
  • Perhaps create an input on the rendered HTML that contain's the JSON serialized array? – Brian Commented Aug 16, 2011 at 19:38
  • Be aware that older browsers have a security hole when JSON arrays are returned directly from a request to the server, which could expose your clients to cross-site scripting attacks. haacked./archive/2008/11/20/… – StriplingWarrior Commented Aug 16, 2011 at 19:46
  • 1 Minor nitpick: what you have is a Java list, not JSON array... but you want to send it as a JSON array. – StaxMan Commented Aug 17, 2011 at 21:16
Add a ment  | 

4 Answers 4

Reset to default 1

ok First:

ArrayList array=new ArrayList();
array.add("D");
array.add("A");
array.add("L");
JSONArray array = new JSONArray();

This can't pile... you have a duplicate variable array ;-)

Second: create a servlet/Struts Action/etc that will contains the code that will create your array. Then transform it in JSON with a JSON library. Finally, put the string in the response of your servlet/Struts Action/etc.

Use JQuery to ease your effort on the Ajax call. It will help you with the Ajax call and the transformation back to Json from the string received in the http response.

Ex:

your ajax call should be like that (with JQuery)

$.getJSON("http://yourserver/JSONServlet",
    function(data){
           alert (data) // this will show your actual json array
      });
    });

and your servlet should look like that:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import net.sf.json.JSONArray;

public class JSONServlet extends  HttpServlet{
  public void doGet(HttpServletRequest request,
  HttpServletResponse response)
   throws ServletException,IOException{
 JSONArray arrayObj=new JSONArray();
 arrayObj.add("D");
 arrayObj.add("A");
 arrayObj.add("L");
 arrayObj.add("D");
 arrayObj.add("A");
 arrayObj.add("TEST");
  PrintWriter out = response.getWriter();
  out.println(arrayObj);
  for(int i=0;i<arrayObj.size();i++){
  out.println(arrayObj.getString(i));
  }
 }
}

you basically use certain classes in java like the ones defined here: http://json/java/ convert the final output to a json string and then send it to javascript there you convert the json string back to json using eval or probably using a library called json2.js and you are all set..

here is code for the same:

JSONObject obj=new JSONObject();
  obj.put("name","foo");
  obj.put("num",new Integer(100));
  obj.put("balance",new Double(1000.21));
  obj.put("is_vip",new Boolean(true));
  obj.put("nickname",null);
  StringWriter out = new StringWriter();
  obj.writeJSONString(out);
  String jsonText = out.toString();
  System.out.print(jsonText);

for more http://code.google./p/json-simple/wiki/EncodingExamples

Instead of using org.json's barebones library as suggested already, consider using data-binding JSON library like Jackson or GSON (there are many others as well), in which case you can simplify servlet case (with Jackson) to:

new ObjectMapper().writeValue(response.getOutputStream(), array);

and that's all you need.

And for even simpler handling, JAX-RS services (Jersey, RESTeasy, CXF) can further simplify handling, to reduce code you need pared to raw servlets. JAX-RS + JSON is the modern way to implement web services on Java, so might make sense to learn it now.

The simplest way is construct a json string in java file and return that json string to client.

Javascript provides a eval() method which inverts json string to json object.

Then you can perform what ever operations you need.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745301068a4621437.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信