I am trying find a Java lib/api that will allow me to turn the contents of a HTTP Request POST body into a JSON object.
Ideally I would like to use a Apache Sling library (as they are exposed in my container naturally).
The closest I've found it: org.apache.sling.commons.json.http
which converts the header to JSON.
HTTP Post bodies are in the format; key1=value1&key2=value2&..&keyn=valueN
so I assume there is something out there, but I havent been able to find it.
I may just have to use a custom JSONTokener
(org.apache.sling.commons.json.JSONTokener
) to do this if something doesn't already exist. Thoughts?
Thanks
Jackson is also a good option - its used extensively in Spring. Here is the tutorial: http://wiki.fasterxml.com/JacksonInFiveMinutes
Use Gson. With this you can create class with private variables which represent the data you want : for example.
Json Object could be retrieve like this :
And your instruction are simple. Content is the content of your Page, you can retrieve it with Asynctask.
Sorry on making this an own answer but obviously my reputation doesn't allow me to simply add a comment to the answer How to convert HTTP Request Body into JSON Object in Java of maerics.
I would also iterate over the request params but instead of using an arbitrary json library use the JSONObject that is provided by sling. http://sling.apache.org/apidocs/sling6/org/apache/sling/commons/json/JSONObject.html
import org.json.JSONObject;
Assuming you're using an HttpServlet and a JSON library like json-simple you could do something like this:
With example usage:
I recommend trying Apache Commons Beanutils.