How can I get a JSONObject
from a HttpServletRequest
in servlets?
Use the json.loads() function. The json. loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.
We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.
A JSONObject constructor can be used to convert an external form JSON text into an internal form whose values can be retrieved with the get and opt methods, or to convert values into a JSON text using the put and toString methods.
Very simple:
JSONObject o = new JSONObject(request.getParameter("WHATEVER"));
Edit: Since you use json-lib, it's
JSONObject o = (JSONObject) JSONSerializer.toJSON(request.getParameter("WHATEVER"));
for you.
It seems like you must be using the net.sf.json.JSONObject
version of JSONObject (this is not the json.org version).
For the net.sf.json.JSONObject
version simply use
JSONObject.fromObject(Object obj)
where obj
is either
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