I am using GWT's JsonpRequestBuilder to issue a cross site REST request whose response is a JSON object.
The callback parameter of requestObject method is a JavaScriptObject. but I do not want to implement a JavaScriptObject, but rather to parse the JSON response directly. Is there anyway I can get the JSON string directly from any method of JavaScriptObject or JsonpRequestBuilder ?
If you want to use the com.google.gwt.json.JSON
module (seriously, you'd better write JavaScriptObject
s, this JSON module is a PITA to work with), then you can simply wrap the returned JavaScriptObject
into a JSONObject
or JSONArray
:
new JSONObject(myJavaScriptObject)
Use requestString instead of requestObject. Like this:
JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
jsonp.requestString(url,
new AsyncCallback<String>() { ...
This will return a String instead of a JavaScriptObject. You can use then use the JSONParser, like this:
JSONObject value = (JSONObject)JSONParser.parseStrict(jsonString);
Person person = new Person();
person.setName(value.get("Name").isString().stringValue());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With