请问怎样将有java.sql.Date属性的Bean转换成json字符串,再把json字符串转换回Bean请大侠指教!!! 怎样从java后台获取json字符串并转换为json对象输出
\u5e26\u6709\u5bf9\u8c61\u5c5e\u6027\u7684bean\uff0c\u5982\u4f55\u8f6c\u6362\u6210json\u5bf9\u8c61\u6216\u8005\u5b57\u7b26\u4e32fastjson\u8f6c\u5316\u5982\u4e0b\uff1a
public class Userinfo {
private String username;
private String password;
private Address address\uff1b
//\u7701\u7565\u76f8\u5e94\u7684get/set\u65b9\u6cd5
}
//json\u8f6c\u5316\u5355\u4e2a\u5bf9\u8c61
Userinfo userinfo = JSON.parseObject(jsonString, Userinfo.class);
//json\u8f6c\u5316\u4e3a\u591a\u4e2a\u5bf9\u8c61
List list = JSON.parseArray(jsonString, Userinfo.class);
\u5bf9\u8c61\u8f6c\u5316\u4e3aJSON
JSON json = JSON.toJSON(userinfo);
\u5728model\u5c42
public List> query() throws SQLException
{
List> listmap=new ArrayList>();
Connection conn=null;
ResultSet rs=null;
String query="SELECT * FROM COURSE";
Statement st=null;
conn=Conn.getConnection();
st=conn.createStatement();
rs=st.executeQuery(query);
while(rs.next())
{
Map map=new HashMap();
map.put("coursetypeid", rs.getString("COURSE_TYPE_ID"));
map.put("coursename", rs.getString("COURSE_NAME"));
map.put("publish", rs.getString("PUBLISH"));
map.put("publishtime", rs.getString("PUBLISH_TIME"));
map.put("coursenumber", rs.getString("CHAPTER_NUMBER"));
map.put("courseid", rs.getString("COURSE_ID"));
map.put("coursedesc", rs.getString("COURSE_DESC"));
listmap.add(map);
}
conn.close();
return listmap;
}
\u63a7\u5236\u5c42
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
ConImp c=new ConImp();
String json= c.QueryCourseInfo();
response.getWriter().write(json);
}
\u6700\u540e\u5728\u9875\u9762\u901a\u8fc7\u8f6c\u6362\u83b7\u5f97\u5e76\u663e\u793a\u3002
http://json-lib.sourceforge.net/usage.html
From Beans to JSON:
class MyBean{
private String name = "json";
private int pojoId = 1;
private char[] options = new char[]{'a','f'};
private String func1 = "function(i){ return this.options[i]; }";
private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
// getters & setters
...
}
JSONObject jsonObject = JSONObject.fromObject( new MyBean() );
System.out.println( jsonObject );
/* prints
{"name":"json","pojoId":1,"options":["a","f"],
"func1":function(i){ return this.options[i];},
"func2":function(i){ return this.options[i];}}
*/
在你这里就是:JSONObject jsonObject = JSONObject.fromObject(user );
From JSON to Beans:
String json = "{bool:true,integer:1,string:\"json\"}";
JSONObject jsonObject = JSONObject.fromObject( json );
BeanA bean = (BeanA) JSONObject.toBean( jsonObject, BeanA.class );
在你这里就是:
JSONObject jsonObject = JSONObject.fromObject( json );
User bean = (User) JSONObject.toBean( jsonObject, User.class );
如果是我,我就会把birthday的类型设置为String类型,并且限定为“yyyy-MM-dd”的形式,然后写个工具方法将字符串与日期进行互解析。
JSON擅长的本来就是字符串,不如投其所好,与其实现他的接口,不如自己实现工具方法,简单快捷。
void setBirthday(String birth){
this.birthday = functionName(birth);
}
String getBirthday(){
return birthday;
}
String functionName(Date date){
return "yyyy-MM-dd";
}
Date funcitonName(String yMd){
return date;
}
json有一个工具叫做Json-lib,提供相应的转换操作。可以从google一下这个工具,挺好用的。
绛旓細浣犻渶瑕佺敤鍒扮涓夋柟鐨刲ib鏂囦欢锛 json-lib銆俬ttp://json-lib.sourceforge.net/usage.html From Beans to JSON锛歝lass MyBean{ private String name = "json";private int pojoId = 1;private char[] options = new char[]{'a','f'};private String func1 = "function(i){ return this.optio...
绛旓細java.sql.Date鏄痡ava.util.Date鐨勫瓙绫汇俲ava.util.Date() dateUtil=new java.util.Date();java.sql.Date dateSql=new java.sql.Date(dateUtil.getTime()); 鑰宩ava.sql.Date杞负java.util.Date灏辩畝鍗曚簡銆俲ava.util.Date() newDate=dateSql;(澶氭) 灏java.util.Date绫诲瀷鐨勬棩鏈熶互YYYY-MM-DD鏍煎紡杞崲涓哄瓧绗...
绛旓細浣犲湪new date()鐨勬椂鍊欓渶瑕佸鍏ュ寘锛岃繖涓寘灏遍java.sql.date灏辫浜嗭紝鑷充簬鎻掑叆鏁版嵁搴擄紝涓鑸琺ysql鍙互鐩存帴鎻掑叆涓嶉渶瑕佽浆鎹紝oracle鍙兘闇瑕佽浆鎹笅锛屽to_date杩欐牱鐨勫嚱鏁板氨鍙互浜
绛旓細瀛楁updateTime涓篋ate绫诲瀷锛岄偅涔堝鏋滃彧瑕佺簿纭埌骞存湀鏃ワ紝閭d箞灏变娇鐢╰runc(updateTime)鍗冲彲锛屽叾浠栬繕鏈夛細trunc[鎴柇鍒版渶鎺ヨ繎鐨勬棩鏈,鍗曚綅涓哄ぉ] ,杩斿洖鐨勬槸鏃ユ湡绫诲瀷 select sysdate S1,trunc(sysdate) S2, //杩斿洖褰撳墠鏃ユ湡,鏃犳椂鍒嗙 trunc(sysdate,'year') YEAR, //杩斿洖褰撳墠骞寸殑1鏈1鏃,鏃犳椂鍒嗙 trun...
绛旓細鍦java.sql.Date绫讳腑鏈変竴涓彨valueOf(String s)鐨勬柟娉曪紝瀹冭繑鍥炵殑鏂规硶灏辨槸浣犺鐨勭被鍨嬶紝涓嶈繃瀹冨浼犲叆鐨凷tring s鏈変竴瀹氱殑鏄剧ず锛屽繀椤绘槸鈥測yyy-mm-dd鈥濇牸寮忕殑鏁版嵁銆傚鏋滀笉鏄埗瀹氭牸寮忕殑鏁版嵁锛屼粬浼氭姏鍑衡淚llegalArgumentException鈥濈殑寮傚父銆俲ava api涓槸濡備笅瑙i噴锛堟垜鐢ㄧ殑api鏄1.6鐗堢殑锛塿alueOf public ...
绛旓細databasename = 鏁版嵁搴撳悕绉2锛塐RACLE (a) thin杩炴帴 椹卞姩瀛楃涓诧細oracle.jdbc.driver.OracleDriver 杩炴帴瀛楃涓诧細JDBC:ORACLE:thin:@涓绘満鍚:绔彛鍙:鏁版嵁搴揝ID (b) oci8杩炴帴 椹卞姩瀛楃涓诧細 oracle.jdbc.driver.OracleDriver 杩炴帴瀛楃涓 锛欽DBC:ORACLE:oci8:@缃戠粶鏈嶅姟鍚3)My SQL 椹卞姩...
绛旓細瑕佽繛鎺ユ暟鎹簱,闇瑕佸悜java.sql.DriverManager璇锋眰骞惰幏寰桟onnection瀵硅薄,璇ュ璞″氨浠h〃涓涓暟鎹簱鐨勮繛鎺ャ 浣跨敤DriverManager鐨刧etConnectin(String url,String username,String password )鏂规硶浼犲叆鎸囧畾鐨勬杩炴帴鐨勬暟鎹簱鐨勮矾寰勩佹暟鎹簱鐨勭敤鎴峰悕鍜屽瘑鐮佹潵鑾峰緱銆 渚嬪://杩炴帴MySql鏁版嵁搴,鐢ㄦ埛鍚嶅拰瀵嗙爜閮芥槸root String url =...
绛旓細String sql = "select * from student";ps = conn.prepareStatement(sql);rs = ps.executeQuery();while(rs.next()){ Student student = new Student();student.setId(rs.getInt(1));student.setName(rs.getString(2));student.setAddress(rs.getString(3));list.add(student);} } catch...
绛旓細import java.sql.ResultSet; //閮芥槸瀵煎叆涓浜涗笅闈㈤渶瑕佺敤鍒扮殑绫 String ConnStr="jdbc:microsoft:sqlserver://WINJUE:1433;DatabaseName=library";//jdbc杩炴帴鏁版嵁搴搖rl public sqlServer() { try { Class.forName(DBDriver).newInstance(); //鍔犺浇SQLServer椹卞姩绋嬪簭,DBDriver鏄綘涓婇潰鐨勪唬鐮;conn=...
绛旓細import java.sql.*; //鍔犺浇java鏁版嵁杩炴帴鍖,java鍩烘湰鎵鏈夌殑鏁版嵁搴撶殑璋冪敤鐨勯兘鍦ㄨ繖涓笢瑗块噷闈 public class InsertCoffees { public static void main(String args[]) { String url = "jdbc:odbc:sqlserver"; //鍙栧緱杩炴帴鐨剈rl鍚,娉ㄦ剰sqlserver鏄痙sn鍚 Connection con; //瀹炰緥鍖栦竴涓狢onnection瀵硅薄 Statement stmt;...