Home > Back-end >  What reason is empty string to a Java object object, turn a json object is normal!
What reason is empty string to a Java object object, turn a json object is normal!

Time:10-02



-- DBConnect. Java
 package com. CMCC. TestUtil; 

Public class DBConnect {
Private String driv;
Private String url;
Private String name;
Private String PSW.

Public DBConnect (d String, the String u, String n, String p) {
Driv=d;
Url=u;
Name=n;
PSW=p;
}

Public String getDriv () {
Return driv;
}

Public void setDriv (String driv) {
This. Driv=driv;
}

Public String getUrl () {
Return the url;
}

Public void setUrl (String url) {
Enclosing the url=url;
}

Public String getName () {
Return the name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

Public String getPsw () {
Return the PSW;
}

Public void setPsw (String PSW) {
This. PSW=PSW;
}

@ Override
Public String toString () {
Return DBConnect {" + "
"Driv='" + driv +' \ ' '+
", url='" + url +' \ ' '+
"And name='" + name +' \ ' '+
", PSW='" + PSW +' \ ' '+
'} ';
}
}


-- -- -- -- -- -- -- -- -- -- -- -- -- - GetDBConn. Java
 package com. CMCC. TestUtil; 

The import com. Alibaba. Fastjson. JSONObject;
The import com.cmcc.com mon. Constant. Constants;
The import com.cmcc.com mon. Utils. DESUtil;

Public class GetDBConn {
Public static void getDBConn () {
String drive="... The oracle JDBC driver OracleDriver ";
The String url="JDBC: oracle: thin: @ localhost: 1521: XE";
String username="MMS";
String password="MMS";

DBConnect conn=new DBConnect (drive, url, username, password);

//display printing field
String json=JSONObject. ToJSONString (conn);
System. Out. Println (objects to json string: "[]" + json + "");

//the string encryption
String mi=DESUtil. Jiami (json, the DB_CONN_KEY);
System. The out. Println (" encryption: [" + mi + "] ");

//to decrypt the string
String Ming=DESUtil. Jiemi (mi, the DB_CONN_KEY);
System. The out. Println (" decryption: [Ming "+ +"] ");

//decrypt the string object


//decryption string json object
JSONObject jobj=JSONObject. ParseObject (Ming);
System. The out. Println (" turn json object: [drive="+ jobj. Get string (" driv") + ", url="+ jobj. Get string (" url") + ", username="+ jobj. Get string (" name") + ", "=" + jobj. Get string (PSW) + "] ");

DBConnect db=JSONObject. ToJavaObject (jobj, DBConnect. Class);
System. The out. Println (" turn object: [" + db. The toString () + "] ");
}

Public static void main (String [] args) {
GetDBConn ();
}
}




-- -- -- -- -- -- -- -- -- -- -- -- result:
Object to a json string: [{" driv ":"... The oracle JDBC driver OracleDriver ", "name" : "MMS", "PSW" : "MMS", "url" : "JDBC: oracle: thin: @ localhost: 1521: XE"}]
Encryption: [Zmecydh8aDAoINl0emY0c3TuKnGtJpMhMCPrJl7r3yxEcIinJlPxn9i + 8 b78ri2krmkjwl8u rW29xXXg/uiK67YXRrYAI9EIO4LcIVlbuLa bluouw3IZwj8zV2uQGO5ztEmq62/JPV2SEdcE4UCg==]
Decryption: [{" driv ":"... The oracle JDBC driver OracleDriver ", "name" : "MMS", "PSW" : "MMS", "url" : "JDBC: oracle: thin: @ localhost: 1521: XE"}]
Turn a json object: [... Drive=the oracle JDBC driver OracleDriver, url=JDBC: oracle: thin: @ localhost: 1521: XE, username=MMS, password=MMS]
Turn objects: [DBConnect {driv='null', url='null' name='null', PSW='null'}]

CodePudding user response:

After you turn json to like why quotes gone?

CodePudding user response:

Why turn json no quotation marks? This is the json format, why decrypted even converted to json? Why not just JSONObjecdt. The parse ()?

CodePudding user response:

Directly from your code will be an error, after the change and no place to be null
 package yyst. MyTest. Test. Lt20; 

The import com. Alibaba. Fastjson. JSONObject;

Public class Test01 {

Public static void main (String [] args) {
GetDBConn. GetDBConn ();
}

}

The class DBConnect {
Private String driv;
Private String url;
Private String name;
Private String PSW.

Public DBConnect () {
//TODO Auto - generated constructor stub
}

Public DBConnect (d String, the String u, String n, String p) {
Driv=d;
Url=u;
Name=n;
PSW=p;
}

Public String getDriv () {
Return driv;
}

Public void setDriv (String driv) {
This. Driv=driv;
}

Public String getUrl () {
Return the url;
}

Public void setUrl (String url) {
Enclosing the url=url;
}

Public String getName () {
Return the name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

Public String getPsw () {
Return the PSW;
}

Public void setPsw (String PSW) {
This. PSW=PSW;
}

@ Override
Public String toString () {
Return DBConnect {" + "
"Driv='" + driv +' \ ' '+
", url='" + url +' \ ' '+
"And name='" + name +' \ ' '+
", PSW='" + PSW +' \ ' '+
'} ';
}
}

The class GetDBConn {
Public static void getDBConn () {
String drive="... The oracle JDBC driver OracleDriver ";
The String url="JDBC: oracle: thin: @ localhost: 1521: XE";
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related