-
Ext User
How do you handle Java JSON return data?
ExtJS JSON and Java Struts:
I like to use struts action calls, unfortunately the Java JSON toString() method returns JSON suited to Java only, that is with backslash escaped quotations and enclosing double quotes.
Everytime I deal with a Java outputted JSON I have to
replace(/\\/g, "")
substring(1, responseText.length - 1)
When I work with TreeLoader I need to override its processResponse, everytime!
Did I miss something fundamental? Am I doing this wrong? I'm sure others using Java have run into the same thing. Is it a bug? Maybe ExtJS should parse for backslashes and enclosing double quotes?
Last edited by Litre; 24 Jan 2010 at 9:46 AM.
Reason: Title
-
Y'wha?
http://json.org/
It just works.
-
Ext User
The dataUrl for my treeLoader calls action OrderTreeMain.action:
<struts>
<package name="almatree_order" extends="struts-default, json-default">
<action name="OrderTreeMain" class="com.almatree.actions.order.OrderTreeAction" method="treeMain">
<result type="json">
<param name="root">treeMainReturn</param>
</result>
</action>
</package>
</struts>
I then set and return the variable treeMainReturn:
setTreeMainReturn(rootJSONArray.toString());
rootJSONArray is a class in library straight from http://www.json.org/java/
What gets passed back by struts is:
"[{\"id\":\"order|2\",\"text\":\"Clarence Liu - ClicPen.ca: 46000053\",\"dragText\":\"2|customerId\",\"allowDrop\":false,\"iconCls\":\"leafIcon\"},{\"id\":\"order|242\",\"text\":\"Clarence Liu - AddMyCard.com: 30000012\",\"dragText\":\"242|customerId\",\"allowDrop\":false,\"iconCls\":\"leafIcon\"}]"
I tried passing the JSONArray directly, by declaring that as the exposed variable, but nothing seems to happen. I assume the problem is I shouldn't be calling toString on the JSONArray but I haven't gotten that to work yet. Does anyone use ExtJS with JSON and struts and know what I'm missing?
Last edited by Litre; 24 Jan 2010 at 2:39 PM.
Reason: grammar
-
Ext User
Your JSON string is getting "escaped" somewhere between toString() and serving by your web container. I'd wager it's somewhere in Struts. Find where strings are getting escaped, and turn it off for that action.
-
Ext User
Ya looks like it, print to stdout shows no backslashes, documentation shows an escape="false" xml tag in property element but not a result or param, sigh... Guess I'll go bug the struts json-plugin forum.
-
Ext User
http://cwiki.apache.org/WW/json-plugin.html
The JSON plugin provides a "json" result type that serializes actions into JSON. The serialization process is recursive, meaning that the whole object graph, starting on the action class (base class not included) will be serialized (root object can be customized using the "root" attribute). If the interceptor is used, the action will be populated from the JSON content in the request, these are the rules of the interceptor:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules