The following code:
[Java]
StringBuilder sb = new StringBuilder ("[");
Sb. append ("{");
Sb. append ("cremindTitle: \" nickname \",");
Sb. append ("cremindDate: \" 19:20:22 \",");
Sb. append ("cremindRemark: \" Stephen \"");
Sb. append ("}");
Sb. append ("]");
List <Cremind> clist = JSONArray. toList (JSONArray. fromObject (sb. toString ()),
Cremind. class );
An error is reported during execution because the conversion from a string of the date type to a Timestamp of the date type is as follows:
Write a Morpher class inherited from AbstractObjectMorpher
[Java]
Class TimeStampMorpher extends actobjectmorpher {
Private Logger LOG = Logger. getLogger (TimeStampMorpher. class );
Private String format = "yyyy-MM-dd hh: mm: ss ";
Public void setFormat (String format ){
This. format = format;
}
Public TimeStampMorpher (){
// TODO Auto-generated constructor stub
}
Public TimeStampMorpher (String format ){
This. format = format;
}
@ Override
Public Object morph (Object dateStr ){
// TODO Auto-generated method stub
Try {
Java. util. Date date = new SimpleDateFormat (format). parse (dateStr. toString ());
Return new Timestamp (date. getTime ());
} Catch (ParseException e ){
// TODO Auto-generated catch block
If (LOG. isInfoEnabled ())
LOG.info ("the date format may be invalid and the current date of the system will be used ");
E. printStackTrace ();
}
Return new java. SQL. Timestamp (System. currentTimeMillis (); // return the default date
}
@ Override
Public Class morphsTo (){
// TODO Auto-generated method stub
Return Timestamp. class;
}
}
Final Version:
[Java]
Package com. defshare. foundation. global;
Import java. SQL. Timestamp;
Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util. List;
Import org. apache. log4j. Logger;
Import net. sf. ezmorph. object. AbstractObjectMorpher;
Import net. sf. json. JSONArray;
Import net. sf. json. util. JSONUtils;
Class TimeStampMorpher extends actobjectmorpher {
Private Logger LOG = Logger. getLogger (TimeStampMorpher. class );
Private String format = "yyyy-MM-dd hh: mm: ss ";
Public void setFormat (String format ){
This. format = format;
}
Public TimeStampMorpher (){
// TODO Auto-generated constructor stub
}
Public TimeStampMorpher (String format ){
This. format = format;
}
@ Override
Public Object morph (Object dateStr ){
// TODO Auto-generated method stub
Try {
Java. util. Date date = new SimpleDateFormat (format). parse (dateStr. toString ());
Return new Timestamp (date. getTime ());
} Catch (ParseException e ){
// TODO Auto-generated catch block
If (LOG. isInfoEnabled ())
LOG.info ("the date format may be invalid and the current date of the system will be used ");
E. printStackTrace ();
}
Return new java. SQL. Timestamp (System. currentTimeMillis (); // return the default date
}
@ Override
Public Class morphsTo (){
// TODO Auto-generated method stub
Return Timestamp. class;
}
}
Public final class JsonUtil {
/**
* Json array string to json array object <br/>
* <Pre>
* StringBuilder sb = new StringBuilder ("[");
* Sb. append ("{");
* Sb. append ("cremindTitle: \" xiaoqiang \",");
* Sb. append ("cremindDate: \" 2012-06-0519: 20: 22 \",");
* Sb. append ("cremindRemark: \" xiaoqiangremark \"");
* Sb. append ("}");
* Sb. append ("]");
* </Pre>
* @ Param jsonArray
* @ Param classzz
*/
Public static final <T> List <T> jsonArrayToList (String jsonArray, Class classzz ){
JSONUtils. getMorpherRegistry (). registerMorpher (new TimeStampMorpher ());
// JSONUtils. getMorpherRegistry (). registerMorpher (new TimeStampMorpher ());
// StringBuilder sb = new StringBuilder ("[");
// Sb. append ("{");
// Sb. append ("cremindTitle: \" nickname \",");
// Sb. append ("cremindDate: \" 2012-06-0519: 20: 22 \",");
// Sb. append ("cremindRemark: \" Stephen \"");
// Sb. append ("}");
// Sb. append ("]");
// List <Cremind> clist = JSONArray. toList (JSONArray. fromObject (sb. toString ()),
// Cremind. class );
Return JSONArray. toList (JSONArray. fromObject (jsonArray), classzz );
}
}
Project Presentation Layer Action call:
[Java]
/**
* New customer
* Request parameters:
* Name: customer name
* Cno: Customer ID (different from the ID)
* CustomKindId: Customer ID
* Phone: company phone number
* Fax: Company fax
* Address: Customer address
* SiteUrl: company website
* CIndustryId: Customer industry ID
* CIndustryNum: industry ranking
* MainSellProduct: Main Product
* CbuildDate: date of establishment
* ClevelId: customer level ID
* SignTag: sign of Contract
* Remind :[{
* CremindTitle: reminder name
* CremindDate: reminder date
* CremindRemark: Remarks
* },{},{}]
* @ Return
* @ Throws Exception
*/
Public String addCustomer () throws Exception {
Return super.exe cuteAction (new IExecuteActionCallback (){
Public String execute (BaseJSONAction baseJsonAction ){
Customer cst = new Customer ();
BindParam2Bean (cst); // bind the request parameter to the object
// Bind special parameters
// Process the customer ID
String customKindId = getParam ("customKindId"). toString ();
If (! StringUtils. isBlank (customKindId )){
CustomKind ck = new CustomKind ();
Ck. setCustomKindId (Integer. valueOf (customKindId ));
Cst. setCustomKind (ck );
}
// Process the customer's Industry
String cIndustryId = getParam ("cIndustryId"). toString ();
If (! StringUtils. isBlank (cIndustryId )){
Cindustry cs = new Cindustry ();
Cs. setCindustryId (Integer. valueOf (cIndustryId ));
Cst. setCindustry (cs );
}
// Process customer level
String clevelId = getParam ("clevelId"). toString ();
If (! StringUtils. isBlank (clevelId )){
Clevel cl = new Clevel ();
Cl. setClevelId (Integer. valueOf (clevelId ));
Cst. setClevel (cl );
}
// Handle Processing Time
Cst. setCregArchDate (DateUtil. getTimestamp ());
If (cst. getCbuildDate () = null)
Cst. setCbuildDate (DateUtil. getTimestamp ());
// Handling staff
Emp emp = getCurrentUser ();
If (emp! = Null)
Cst. setEmp (emp );
<Span style = "color: # 3366ff;"> // handle alert
String remind = getParam ("remind"). toString ();
// Deserialize the JSON string to the object Array
// List <Cremind> clist = JSONArray. toList (JSONArray. fromObject (remind), Cremind. class );
List <Cremind> clist = JsonUtil. jsonArrayToList (remind, Cremind. class); </span>
// Set customer reminder
Cst. setCreminds (new HashSet (clist ));
CstService. addCustomer (cst );
Return SUCCESS;
}
});
}
Author: jiangtongcn