How can we return a JSON object from a Java Servlet

0 votes
Is there any way to return a JSON object from a Java Servlet?
Jul 6, 2018 in Java by Daisy
• 8,140 points
5,879 views

1 answer to this question.

0 votes
response.setContentType("application/json");
// Get the printwriter object from response to write the required json object to the output stream      
PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following, it will return your json object  
out.print(jsonObject);
out.flush();
answered Jul 6, 2018 by sharth
• 3,370 points

Related Questions In Java

0 votes
2 answers

How can we remove an element from an array in Java?

You can use ArrayUtils class remove method ...READ MORE

answered May 24, 2018 in Java by UshaK
2,728 views
0 votes
1 answer

How can we remove html tags from a string?

You can use HTML parser. This is ...READ MORE

answered Jun 4, 2018 in Java by sophia
• 1,400 points
4,186 views
0 votes
1 answer

How can we split a comma separated String in Java?

String str = "..."; // write the ...READ MORE

answered Jun 12, 2018 in Java by Parth
• 4,640 points
2,372 views
0 votes
1 answer

How can we set environment variables from java?

For setting the environment variable, you can ...READ MORE

answered Jun 21, 2018 in Java by Akrati
• 960 points
3,822 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,549 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
3,251 views
0 votes
1 answer

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

You can use: new JSONObject(map); READ MORE

answered Jun 27, 2018 in Java by Akrati
• 3,190 points
6,990 views
0 votes
2 answers

How do we convert JSON to Map in Java

When you don't know structure of json. ...READ MORE

answered Oct 31, 2018 in Java by Sushmita
• 6,920 points
6,009 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,299 views
0 votes
6 answers

How can we define global variables in java?

To define Global Variable you can make ...READ MORE

answered Dec 15, 2020 in Java by Gitika
• 65,770 points
117,813 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP