<< Improve Form Usability | Home | DRY CRUD DAOs with JPA >>

jqGrid JSON Java Model

I've been using the JQuery plugin jqGrid for several months now. I've been really pleased with it. Up to this point, due to laziness, I've been building my JSON string manually using StringBuilder. Tonight I decided it was time to make this easier.

jqGrid accepts different forms of data to represent the grid. I prefer to use JSON because its very light weight and transforming Java objects to JSON is a light weight task. Here is an example of a JSON string used by jqGrid. It is very simple. It only contains two columns of data and a handful of rows.

{"page":"1","records":5,"rows": [ {"cell":["Blue","This is blue"],"id":1}, {"cell":["Green","This is green"],"id":2}, {"cell":["Re","This is red"],"id":3}, {"cell":["Black","This is Black"],"id":4}, {"cell":["Purple","This is purple"],"id":5} ],"total":"1"}

To model this string in java I had to create two objects. JQGridJSONModel.java and JQGridRow.java. See the code below.

public class JQGridJSONModel { private String page; private String total; private Integer records; private List<JQGridRow> rows; // getters and setters omitted } public class JQGridRow { private Integer id; private List<String> cell; // getters and setters omitted }

Using these classes is quite simple. To generate the JSON string above I have a Color class that contains an id, name, and description. Once I retrieve the list from the database I do the following.

JQGridJSONModel json = new JQGridJSONModel(); json.setPage("1"); json.setRecords(colors.size()); json.setTotal("1"); List<JQGridRow> rows = new ArrayList<JQGridRow>(); for (Color c : colors) { JQGridRow row = new JQGridRow(); row.setId(c.getId()); List<String> cells = new ArrayList<String>(); cells.add(c.getName()); cells.add(c.getDescription()); row.setCell(cells); rows.add(row); } json.setRows(rows);

Then just use your favorite JSON serializer to generate your JSON string. Just make sure you are able to exclude the class parameter that generally gets thrown into the serialization process. I really like FlexJSON.

JSONSerializer serializer = new JSONSerializer(); String jsonResult = serializer.exclude("*.class").deepSerialize(json);

That's it. Feel free to do with this as you wish



Re: jqGrid JSON Java Model

Good post. FYI, I could only get this to work if the "id" values in colModel are quoted, e.g. "id":"1"

Re: jqGrid JSON Java Model

Sorry, didn't mean colModel, but in the JSON result. I had to make id a String in the model so that the JSON serializer would spit out quoted id values, as required by jqGrid.

Re: jqGrid JSON Java Model

Hi iam new to jquery and i am trying to mix jquery with jsp.i don't know how to do that can u please send peace of code both html and jsp

Re: jqGrid JSON Java Model

Hi, I have been trying to understand the jqgrid with java. If you have handy example can you please share it with me? I am looking at a solution wtih pagination and sort features and bound to struts 2. Thanks

Re: jqGrid JSON Java Model

Hi, I'm trying to use jqGrid with struts2. I used the json plugin, I have an action: <action name="jsontest" class="home.actions.JsonTestAction"> <result type="json"/> </action> But the table gets rendered with no data. Any ideas? Thank you

Re: jqGrid JSON Java Model

could you send me a copy of jqgrid. I can't to download it from web. thanks. my email is hosoft@foxmail.com your artical is very usefule