Java将Java对象转换为Json对象

时间:2019-03-28 09:04:39

标签: java json

我无法将Java对象转换为JSON对象,这是我的主要Java对象:

我这样做:

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

但是在输出上我没有这个:

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

    "app_info":
    {
        "os":"soapui",
        "ver":1,
        "lang":"pl"
    }
}

3 个答案:

答案 0 :(得分:1)

如果需要以上输出,请尝试以下操作:

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

答案 1 :(得分:1)

您可以创建两个DAO类,

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

然后您就可以这样做

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

如果没有,您可以简单地做到这一点,

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

答案 2 :(得分:1)

您可以通过更多方式执行此操作。如下所示:

使用Google Gson

Maven依赖项:

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

Java代码:

LoginDao loginData; 

// Here  loginData is the object. ...

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

使用Jacson

等级依赖性

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

Java代码

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(loginData);