Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get JSON string by using JsonpRequestBuilder

Tags:

jsonp

gwt

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 ?

like image 902
Gu. Avatar asked Feb 25 '23 08:02

Gu.


2 Answers

If you want to use the com.google.gwt.json.JSON module (seriously, you'd better write JavaScriptObjects, 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)
like image 64
Thomas Broyer Avatar answered Mar 04 '23 10:03

Thomas Broyer


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());
like image 23
Brad Rydzewski Avatar answered Mar 04 '23 10:03

Brad Rydzewski