Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8997873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:56:52+00:00 2026-06-15T23:56:52+00:00

Imagine a simple JSON response such as: { success: false, message: PEBKAC } Given

  • 0

Imagine a simple JSON response such as:

{
    "success": false,
    "message": "PEBKAC"
}

Given I have boolean and String variables, what’s the simplest way to convert them to JSON in Java, without resorting to String.format and friends.

I’m more familiar with C#, where this is quite straightforward using the built-in JavaScriptSerializer class:

var success = false;
var message = "PEBKAC";
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(new { success, message });

Is there anything this straightforward for Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:56:53+00:00Added an answer on June 15, 2026 at 11:56 pm

    using JSON
    serialization

      org.json.JSONObject obj = new org.json.JSONObject();
      obj.put("success", false);
      obj.put("message", "PEBKAC");
      obj.toString(); 
    

    deserialization

    org.json.JSONObject obj = new org.json.JSONObject(responseAsString);  
    obj.optBoolean("success"); // false
    obj.optString("message"); // PEBKAC
    

    using google-gson

    public class MyObject
    {
       private String message;
       private boolean success;
       public MyObject(String message, boolean success)
       {
          this.message = message;
          this.success = success;
       }
    }  
    

    serialization

       MyObject obj = new MyObject("PEBKAC", false);  
       new com.google.gson.Gson().toJSON(obj);
    

    deserialization

       MyObject obj = new com.google.gson.Gson().fromJSON(responseAsString, MyObject.class);
       obj.getMessage();
       obj.getSuccess();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's imagine really simple game... We have a labirinth and two players trying to
imagine the following simple Models (example for simplicity reasons; in fact, we have MVVM
I imagine this must have a simple answer, but I am struggling: I want
Let's imagine this simple, I have a div (myDiv) inside a form which includes
Imagine a simple Action with a post public ActionResult Unsubscribe(string contentId) { // get
Imagine I have a simple 4x3x2 array in R. > x <- array(1:24,c(4,3,2), dimnames=list(c('a','b','c','d'),c('x','y','z'),1:2))
Imagine this simple piece of code: class Constructor() { var string: String = _
For example, imagine a simple mutator that takes a single boolean parameter: void SetValue(const
Imagine two simple java applications. Both of them are implementing the same JAR file
Imagine a simple page with a list of users. Selecting a user displays a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.