如何通过解析json数据来检索javascript中struts动作发送的值

时间:2014-12-19 06:20:57

标签: javascript jquery json gson struts-1

我是新手。我正在使用httpservlet响应从struts动作发送数据。如何在javascript中检索此值以将这些值设置为html元素。

使用Struts操作和httpservlet对象写入响应。

Gson gson = new Gson(); //using gson-2.3.1.jar 
//log.debug("Printing values ... ");
//log.debug(gson.toJson( languagesResponse.getLanguages()));

response.setContentType("application/json");
// Get the printwriter object from response to write the required json object to the output stream      
PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following, it will return your json object  
out.print(gson.toJson( languagesResponse.getLanguages()));
out.flush();

让json将值返回为:

[{ “stringCode”: “缺省”, “numberCode”:0, “__ hashCodeCalc”:假},{ “stringCode”: “EN_US”, “numberCode”:1, “__ hashCodeCalc”:假}]

我想为javascript elemtent获取此值。

1 个答案:

答案 0 :(得分:0)

您对HTTP服务器的响应与此问题无关,因为您要求的是客户端代码。此外,这可以看作是多个问题的重复,您可以从中获得答案,如this(对该想法有很好的概述)。您还可以查看jQuery的这两部分以获取实际的代码示例:AJAX& DOM manipulation