如何在Struts中使用其键值获取json对象?

时间:2022-03-11 11:17:50

I am working on web services in struts. Now I want json object using its key value. Then I have to post something like array in response. I have no idea how to do that in Struts. I know how to do it in Servlets. So, I am using the following code I have tried, but I think it is different in Struts.

我正在struts中处理Web服务。现在我想要使用其键值的json对象。然后我必须在响应中发布类似数组的内容。我不知道如何在Struts中这样做。我知道如何在Servlets中做到这一点。所以,我正在使用我尝试过的以下代码,但我认为它在Struts中有所不同。

JSONObject json = (JSONObject)new JSONParser().parse(jb.toString());
                      String key_value= json.get("key").toString(); 

So, how to do it in Struts. Please also tell me how to parse json array in response.

那么,如何在Struts中完成它。还请告诉我如何解析json数组作为响应。

1 个解决方案

#1


1  

Working with JSON not necessary to send JSON to Struts. Even if it could be configured to accept JSON content type, it won't help you. You can use ordinary request to Struts with the data passed in it. If it's an Ajax call then you can use something like

使用JSON不需要将JSON发送到Struts。即使它可以配置为接受JSON内容类型,它也无济于事。你可以使用普通的Struts请求和传入的数据。如果它是一个Ajax调用,那么你可以使用类似的东西

$.ajax({
   url: "<s:url namespace="/aaa" action="bbb"/>",     
   data : {key: value},
   dataType:"json",
   success: function(json){
     $.each(json, function( index, value ) {
       alert( index + ": " + value );
     });
   }
}); 

The value should be an action property populated via params interceptor and OGNL. The json returned in success function should be JSON object and could be used directly without parsing.

该值应该是通过params拦截器和OGNL填充的action属性。成功返回的json函数应该是JSON对象,可以直接使用而无需解析。

You need to provide action configuration and setter for the property key.

您需要为属性键提供操作配置和设置器。

struts.xml:

<package name="aaa" namespace="/aaa"  extends="json-default">
  <action name="bbb" class="com.bbb.Bbb" method="ccc">
   <result type="json">
     <param name="root">
   </result>
  </action> 
</package>

This configuration is using "json" result type from the package "json-default", and it's available if you use struts2-json-plugin.

此配置使用包“json-default”中的“json”结果类型,如果您使用struts2-json-plugin,则它可用。

Action class:

public class Bbb extends ActionSupport {

  private String key;
  //setter

  private List<String> value = new ArrayList<>();
  //getter

  public String ccc(){
    value.add("Something");
    return SUCCESS;
  }
} 

When you return SUCCESS result, Struts will serialize a value property as defined by the root parameter to the JSON result by invoking its getter method during serialization.

当您返回SUCCESS结果时,Struts将通过在序列化期间调用其getter方法将根参数定义的值属性序列化为JSON结果。

If you need to send JSON to Struts action you should read this answer.

如果您需要将JSON发送到Struts操作,您应该阅读此答案。

#1


1  

Working with JSON not necessary to send JSON to Struts. Even if it could be configured to accept JSON content type, it won't help you. You can use ordinary request to Struts with the data passed in it. If it's an Ajax call then you can use something like

使用JSON不需要将JSON发送到Struts。即使它可以配置为接受JSON内容类型,它也无济于事。你可以使用普通的Struts请求和传入的数据。如果它是一个Ajax调用,那么你可以使用类似的东西

$.ajax({
   url: "<s:url namespace="/aaa" action="bbb"/>",     
   data : {key: value},
   dataType:"json",
   success: function(json){
     $.each(json, function( index, value ) {
       alert( index + ": " + value );
     });
   }
}); 

The value should be an action property populated via params interceptor and OGNL. The json returned in success function should be JSON object and could be used directly without parsing.

该值应该是通过params拦截器和OGNL填充的action属性。成功返回的json函数应该是JSON对象,可以直接使用而无需解析。

You need to provide action configuration and setter for the property key.

您需要为属性键提供操作配置和设置器。

struts.xml:

<package name="aaa" namespace="/aaa"  extends="json-default">
  <action name="bbb" class="com.bbb.Bbb" method="ccc">
   <result type="json">
     <param name="root">
   </result>
  </action> 
</package>

This configuration is using "json" result type from the package "json-default", and it's available if you use struts2-json-plugin.

此配置使用包“json-default”中的“json”结果类型,如果您使用struts2-json-plugin,则它可用。

Action class:

public class Bbb extends ActionSupport {

  private String key;
  //setter

  private List<String> value = new ArrayList<>();
  //getter

  public String ccc(){
    value.add("Something");
    return SUCCESS;
  }
} 

When you return SUCCESS result, Struts will serialize a value property as defined by the root parameter to the JSON result by invoking its getter method during serialization.

当您返回SUCCESS结果时,Struts将通过在序列化期间调用其getter方法将根参数定义的值属性序列化为JSON结果。

If you need to send JSON to Struts action you should read this answer.

如果您需要将JSON发送到Struts操作,您应该阅读此答案。