This is a tutorial that shows how to handle a JSON Object that is received in a Struts2 Action. This Example is based on the Struts2 Grid Showcase for the Struts2 jQuery Plugin with Grid Extension.

1. First we need to enable the Multi Search Feature for the Grid Component.

< sjg :grid 
	id="customerstable" 
	...
	navigatorSearch="true"
	navigatorSearchOptions="{multipleSearch:true}"
	...
>

Now we can use the multiple search feature like this.

struts2 jquery grid with multiple search

2.) After this we need a String property filter in our Struts2 Action.

private String filters;

public void setFilters(String filters) {
	this.filters = filters;
}

3.) Now we receive a JSON Object when we use the Search in the Navigator of the Grid. The JSON looks like this example.

{
	"groupOp":"AND",
	"rules":[
		{
			"field":"customernumber",
			"op":"lt",
			"data":"200"
		}
		,
		{
			"field":"country",
			"op":"eq",
			"data":"USA"
		}
		]
}

4.) To work with this, we need to serialize this as a JSONObject.

JSONObject jsonFilter = (JSONObject) JSONSerializer.toJSON( filters );

5.) Now we can get the value from the JSON Object. First we get the parameter groupOp.

String groupOp = jsonFilter.getString("groupOp");
log.debug("groupOp :" + groupOp);

6.) Now we require the rules as JSONArray and the size of this array.

JSONArray rules = jsonFilter.getJSONArray("rules");
int rulesCount = JSONArray.getDimensions(rules)[0];
log.debug("Count Rules :" + rulesCount);

7.) In a simple for-loop we can get the values of the rule.

for (int i = 0; i < rulesCount; i++) {
	JSONObject rule = rules.getJSONObject(i);
	log.debug("field :" + rule.getString("field"));
	log.debug("op :" + rule.getString("op"));
	log.debug("data :" + rule.getString("data"));
}

Now you should be able to use this values to build a custom SQL
query or build a JPA / Hibernate criteria.

How to parse a JSON Object when using multiSearch for jqGrid with Java
Tagged on:                         

25 thoughts on “How to parse a JSON Object when using multiSearch for jqGrid with Java

  • 2010-06-29 at 10:20
    Permalink

    hi 🙂 can you tell me reference of JSONObject???????????
    i cant find it !!!!!!

    Reply
  • 2010-11-30 at 10:03
    Permalink

    My program always aborted without any error message when it run to the following code. All subsequence codes are ignored.

    JSONObject jsonFilter = (JSONObject) JSONSerializer.toJSON( filters );

    Much appreciated if you could kindly help me to solve the problem above.

    Thank you!

    Reply
    • 2010-11-30 at 10:23
      Permalink

      can you please post the error codes?
      how does your filter string looks?

      Reply
  • 2010-12-01 at 02:21
    Permalink

    Hi jogep, thanks for your reply.

    My error codes are as following:-

    private String filters;

    public void setFilters(String filters) {
    this.filters = filters;
    }

    …………….
    …………….

    public String execute() throws Exception {
    if (this.filters != null) {
    System.out.println(“filters = ” + this.filters);
    jsonFilter = (JSONObject) JSONSerializer.toJSON(this.filters); //<——– (the codes below this line never run.)
    System.out.println("after Json 1");
    groupOp = jsonFilter.getString("groupOp");
    System.out.println("groupOp :" + groupOp);
    rules = jsonFilter.getJSONArray("rules");
    rulesCount = JSONArray.getDimensions(rules)[0];
    System.out.println("rulesCount = " + rulesCount);
    }
    System.out.println("after Json 2");
    return SUCCESS;
    }

    The filter String is: {"groupOp":"AND","rules":[{"field":"remark","op":"bw","data":"test"}]}

    Thank you!

    Reply
    • 2010-12-03 at 21:28
      Permalink

      your code looks good, can you please surround this line

      jsonFilter = (JSONObject) JSONSerializer.toJSON(this.filters);

      with an try catch block and post your error message?

      are you sure the json lib is in your deployed classpath?

      Reply
  • 2010-12-03 at 14:53
    Permalink

    What jars are you using for the JSONObject and JSONSerializer? I was searching around and found a few different jar files.

    Reply
  • Pingback:How to parse a JSON Object when using multiSearch for jqGrid with Java

  • 2011-04-29 at 04:12
    Permalink

    Hi jogep,

    I have tried your suggestion to surround this line jsonFilter = (JSONObject) JSONSerializer.toJSON(this.filters); with an try catch block as below:-

    try {
    jsonFilter = (JSONObject) JSONSerializer.toJSON(filters);
    } catch (JSONException e) {
    e.printStackTrace();
    }

    But the result is same. It still aborted without any error message.

    The json lib is placed in the WEB-INFlib as other jar files.

    Much appreciated if you could kindly help me to solve the problem above.

    Thank you very much!

    Reply
    • 2011-10-10 at 15:14
      Permalink

      I had the same problem (Struts2) and eventually to speed up testing created a Java class with Main method. When I ran the code above an error was thrown saying the class ezmorph was missing. I added ezmorph-1.0 to the library and all is working.

      Reply
  • 2011-07-22 at 07:35
    Permalink

    hi.
    How can I get JSON from action in grid.Such as:

    action response the json:
    {“genders”:”MALE:男;FEMALE:女”,”gridModel”:[{“affiliatedOrganization”:{“address”:”广州市白云区长腰岭村”,”email”:”xxx@126.com”,”homePage”:”www.xxx.com”,”id”:1,”name”:”长腰岭一队”,”new”:false,”organizationCategory”:”OWNER”,”tel”:”020-88888888″,”version”:1},”email”:”xxx@126.com”,”gender”:”FEMALE”,”id”:1,”mobile”:”13888888888″,”name”:”李刚”,”new”:false,”qq”:”888888″,”tel”:”020-88888888″,”title”:”经理”,”version”:1}],”loadonce”:false,”organisations”:null,”page”:1,”records”:1,”rows”:15,”searchField”:null,”searchOper”:null,”searchString”:null,”sidx”:””,”sord”:”asc”,”total”:1,”totalrows”:null}

    the jsp:
    <sjg:grid
    id="gridedittable"
    caption="%{getText('organization.list')}"
    dataType="json"
    href="%{remoteurl}"
    gridModel="gridModel"
    …..

    I want to get the "genders"'s value,but I can't!
    Thank you!

    Reply
  • 2011-08-25 at 09:47
    Permalink

    Is there a way to prefill filters on page load ?

    is there any option like :

    navigatorSearchOptions=”{multipleSearch:true, initFilters:%{filters}}”

    Reply
    • 2011-08-26 at 07:47
      Permalink

      OGNL should work. You just need a getFilters() method in your grid action.

      Reply
  • 2011-11-21 at 09:39
    Permalink

    hi
    rules = jsonFilter.getJSONArray(“rules”);does not work.

    get such message:”net.sf.json.JSONException: JSONObject[“rules”] is not a JSONArray.”

    can you tell me why?

    thanks.

    Reply
  • 2011-11-22 at 05:03
    Permalink

    hi
    I change your code,it’s work.

    your code:
    JSONArray rules = jsonFilter.getJSONArray(“rules”);
    int rulesCount = JSONArray.getDimensions(rules)[0];
    log.debug(“Count Rules :” + rulesCount);

    my code:
    Object rules_string =jsonFilter.get(“rules”);
    JSONArray rules = (JSONArray) JSONSerializer.toJSON( rules_string);
    int rulesCount = JSONArray.getDimensions(rules)[0];
    log.debug(“Count Rules :” + rulesCount);

    ok.problem resolved.

    bye.

    Reply
  • 2012-01-13 at 05:19
    Permalink

    Hi jogep,I have a problem that the property filters is always null in my grid action.But I can’t find zhe problem.

    private String filters;
    public void setFilters(String filters) {
    this.filters = filters;
    }

    Reply
  • 2012-01-13 at 06:04
    Permalink

    I use IE 7. And the the parameter “filters” always null.What’s the ploblem?

    Reply
  • 2012-07-23 at 09:53
    Permalink

    Yes It is Working My Problem is Sloved

    Reply
  • 2012-08-16 at 09:29
    Permalink

    Hi,

    I am using struts2 jquery grid in my application.

    My problem is
    i want to populate grid columns dynamically?
    So

    How to read json string values in struts2 jsp page.

    My jsonString like this:

    {“al”:[“id”,”data”,”disc”],”data”:null,”disc”:null,”grid”:[{“data”:”data1″,”disc”:”one”,”id”:”1″},{“data”:”data2″,”disc”:”two”,”id”:”2″},{“data”:”data3″,”disc”:”three”,”id”:”3″}],”gridmodel”:[{“data”:”data1″,”disc”:”one”,”id”:”1″},{“data”:”data2″,”disc”:”two”,”id”:”2″},{“data”:”data3″,”disc”:”three”,”id”:”3″}],”id”:null}

    i want “al” in my jsp

    For that i write code in my jsp…

    jsp
    ————-

    How to access “al” in strust2 jsp

    Thank you

    Reply
  • 2014-04-15 at 17:50
    Permalink

    Thank very much for your tutorial! It is helpful to you to solve problem in my project !!!

    Reply
  • 2014-05-07 at 15:21
    Permalink

    Dear Jogep,
    thank you so much for this helpfull code.
    Unfortunately I have the same issue of Xiey, i.e. i always retrieve NULL filters in my action.

    This is my grid

    and in the action

    private String filters;

    public String getFilters() {
    return filters;
    }
    public void setFilters(String filters) {
    this.filters = filters;
    }

    ….

    public String execute()
    {
    }
    if (this.filters!=null){
    System.out.println(“JSON filters NOT NULL: ” + this.filters);
    JSONObject jsonFilter = (JSONObject) JSONSerializer.toJSON( this.filters );
    ….}else
    System.out.println(“JSON filters NULL: ” );

    Could you kindly support me?
    Is there something I miss?

    Thanks in advance and best regards

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.