Java 将 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"

    }

}


喵喵时光机
浏览 252回答 3
3回答

PIPIONE

您可以通过更多方式执行此操作。下面给出了以下几个方面:使用 Google Gson:Maven dependency:<dependency>&nbsp; &nbsp; <groupId>com.google.code.gson</groupId>&nbsp; &nbsp; <artifactId>gson</artifactId>&nbsp; &nbsp; <version>2.8.0</version></dependency>Java 代码:LoginDao loginData;&nbsp;// Here&nbsp; loginData is the object. ...Gson gson = new Gson();String json = gson.toJson(loginData);使用杰克逊:Gradle Dependency:compile 'com.fasterxml.jackson.core:jackson-databind:2.5.3'Java 代码ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();String json = ow.writeValueAsString(loginData);

开满天机

如果您需要上述输出,请尝试以下操作: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);

蛊毒传说

您可以创建两个 DAO 类,public class LoginDAO {&nbsp; private String company;&nbsp; private String user;&nbsp; private String secure_password;&nbsp; private String secure_device_id;&nbsp;// Getter Methods&nbsp;&nbsp; public String getCompany() {&nbsp; &nbsp; return company;&nbsp; }&nbsp; public String getUser() {&nbsp; &nbsp; return user;&nbsp; }&nbsp; public String getSecure_password() {&nbsp; &nbsp; return secure_password;&nbsp; }&nbsp; public String getSecure_device_id() {&nbsp; &nbsp; return secure_device_id;&nbsp; }&nbsp;// Setter Methods&nbsp;&nbsp; public void setCompany( String company ) {&nbsp; &nbsp; this.company = company;&nbsp; }&nbsp; public void setUser( String user ) {&nbsp; &nbsp; this.user = user;&nbsp; }&nbsp; public void setSecure_password( String secure_password ) {&nbsp; &nbsp; this.secure_password = secure_password;&nbsp; }&nbsp; public void setSecure_device_id( String secure_device_id ) {&nbsp; &nbsp; this.secure_device_id = secure_device_id;&nbsp; }}public class App_info {&nbsp; private String os;&nbsp; private float ver;&nbsp; private String lang;&nbsp;// Getter Methods&nbsp;&nbsp; public String getOs() {&nbsp; &nbsp; return os;&nbsp; }&nbsp; public float getVer() {&nbsp; &nbsp; return ver;&nbsp; }&nbsp; public String getLang() {&nbsp; &nbsp; return lang;&nbsp; }&nbsp;// Setter Methods&nbsp;&nbsp; public void setOs( String os ) {&nbsp; &nbsp; this.os = os;&nbsp; }&nbsp; public void setVer( float ver ) {&nbsp; &nbsp; this.ver = ver;&nbsp; }&nbsp; public void setLang( String lang ) {&nbsp; &nbsp; this.lang = lang;&nbsp; }}然后你可以这样做,LoginDAO&nbsp; login = new LoginDAO();App_info app = new App_info();JSONObject jo = new JSONObject();&nbsp;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);&nbsp;m.put("os", app.getOs());&nbsp;m.put("ver", app.getVer());&nbsp;m.put("lang", app.getLang());jo.put("app_info", m);&nbsp;System.out.println(jo.toString);如果没有,你可以简单地做到这一点,JSONObject jo = new JSONObject(&nbsp; "{ \"company\":\"\", \"user\":\"test\", \"secure_password\":\"\", \"secure_device_id\":\"\", \"app_info\": { \"os\":\"soapui\", \"ver\":1, \"lang\":\"pl\" } }");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java