JavaEE : Get JSON object from servlet

import com.google.gson.JsonObject; import com.google.gson.JsonParser; public class MyServlet extends HttpServlet{ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { BufferedReader br; StringBuilder data=new StringBuilder(); String line=""; String jsonStr=""; JsonParser parser= new JsonParser(); // Get json string from request try { br = req.getReader(); while (( line= r.readLine()) != null) { data.append(line); } } catch (IOException e) { e.printStackTrace(); } jsonStr=data.toString(); // Get json object JsonObject json = (JsonObject) parser.parse(jsonStr); } }
Method to get application/json from request.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.