2014-12-19 39 views
0

我是新手。我使用httpservlet响应从struts动作发送数据。我应该怎么做才能在javascript中检索这个值来将这些值设置为html元素。如何通过解析json数据来检索从javascript中的struts动作发送的值

使用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”:false}]

我想为javascript elemtent获取此值。

回答

0

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

+0

我知道它的客户端代码来处理,但我不知道我应该怎么在客户端获得响应数据.. – k00989 2014-12-19 06:47:13

+0

你有没有检查我提供的链接?那些应该有相当广泛的材料来处理你所问的所有问题。 – 2014-12-19 06:48:22

相关问题