简体   繁体   中英

Java convert Java object to Json object

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"
    }
}

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);

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);

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\" } }"
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM