handling JSON via play framework 1.2.3
-
12-12-2019 - |
Question
I am sending a JSON message via XHR in a post request (content type: application/json; charset=UTF-8 - I am using firefox/chrome).
However, I am unable to process/receive the JSON message in the play 1,.2.3 controller - probably due to pilot error. Any insights/working example would be appreciated - thanks
JSON message (validates successfully in JSONLint):
{"email":"admin1@test.com","password":"admin123"}
Should I be looking at TypeBinder or create the object from the request parameters?
Solution 3
I ended up iterating through request.params.allSimple in order to get the value via request.params.get. I used the JsonParser().parse on this value to get the JSONElement. I then used getAsJsonObject() on the JSONElement to get the JSONBody. I retrieved the needed JSONElements through a get call on the JSONBody & then a getAs method call to get to the actual value. I will investigate TypeBinders in more detail - any other suggestions would be most welcome.
OTHER TIPS
You can use gson that is embedded in play to parse the param. In your controller method
MyParam myParam = new GsonBuilder().create().fromJson(request.params.get("myParam"), MyParam.class);
You could also try using FLEXJSON (http://flexjson.sourceforge.net/) in order to avoid running into any circular loop errors, especially when