1

I can not convert Java object to JSON object this is my main java object :

I do this:

   public class LoginDao {

        String company;
        String user;
        String secure_password;
        String secure_device_id;
        app_info app_info;
    }

  jsonObject.put("company", company);
            jsonObject.put("user", user);
            jsonObject.put("os", os);
            jsonObject.put("ver", ver);
            jsonObject.put("lang", lang);

but on output I do not have this :

{
    "company":"",
    "user":"test",
    "secure_password":"",
    "secure_device_id":"",

    "app_info":
    {
        "os":"soapui",
        "ver":1,
        "lang":"pl"
    }
}
2
  • You can always use GSON if you don't want to spend too much time on this. Commented Mar 28, 2019 at 9:46
  • Check it out on this GitHub link github.com/google/gson Commented Mar 28, 2019 at 9:53

3 Answers 3

4

You can do this in many more way. Here are given bellow:

Using Google Gson:

Maven dependency:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.0</version>
</dependency>

Java code:

LoginDao loginData; 

// Here  loginData is the object. ...

Gson gson = new Gson();
String json = gson.toJson(loginData);

Using Jackson:

Gradle Dependency:

compile 'com.fasterxml.jackson.core:jackson-databind:2.5.3'

Java code

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(loginData);
Sign up to request clarification or add additional context in comments.

2 Comments

Note that for Gradle dependency there is unnecessary space between databind and version. With it the build fails.
@jinah, Thanks bro for findings. I already corrected this changes
1

If you need above output, try this:

JSONObject obj = new JSONObject();
obj.put("company", company);
obj.put("user", user);
obj.put("secure_password", secure_password);
obj.put("secure_device_id", secure_device_id);

JSONObject anothetObj = new JSONObject();
anothetObj.put("os", os);
anothetObj.put("ver", ver);
anothetObj.put("lang", lang);
obj.put("app_info", anothetObj);

Comments

0

You can create two DAO Classes,

public class LoginDAO {
  private String company;
  private String user;
  private String secure_password;
  private String secure_device_id;


 // Getter Methods 

  public String getCompany() {
    return company;
  }

  public String getUser() {
    return user;
  }

  public String getSecure_password() {
    return secure_password;
  }

  public String getSecure_device_id() {
    return secure_device_id;
  }

 // Setter Methods 

  public void setCompany( String company ) {
    this.company = company;
  }

  public void setUser( String user ) {
    this.user = user;
  }

  public void setSecure_password( String secure_password ) {
    this.secure_password = secure_password;
  }

  public void setSecure_device_id( String secure_device_id ) {
    this.secure_device_id = secure_device_id;
  }
}

public class App_info {
  private String os;
  private float ver;
  private String lang;


 // Getter Methods 

  public String getOs() {
    return os;
  }

  public float getVer() {
    return ver;
  }

  public String getLang() {
    return lang;
  }

 // Setter Methods 

  public void setOs( String os ) {
    this.os = os;
  }

  public void setVer( float ver ) {
    this.ver = ver;
  }

  public void setLang( String lang ) {
    this.lang = lang;
  }
}

An then you can do this,

LoginDAO  login = new LoginDAO();
App_info app = new App_info();

JSONObject jo = new JSONObject(); 

jo.put("company", login.getCompany());
jo.put("user", login.getUser());
jo.put("secure_password", login.getSecure_password());
jo.put("secure_device_id", login.getSecure_device_id());

Map m = new LinkedHashMap(3); 
m.put("os", app.getOs()); 
m.put("ver", app.getVer()); 
m.put("lang", app.getLang());

jo.put("app_info", m); 
System.out.println(jo.toString);

If not you can simply do this,

JSONObject jo = new JSONObject(
  "{ \"company\":\"\", \"user\":\"test\", \"secure_password\":\"\", \"secure_device_id\":\"\", \"app_info\": { \"os\":\"soapui\", \"ver\":1, \"lang\":\"pl\" } }"
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.