JSON object usage includes date formatting

Source: Internet
Author: User
Tags date1 tojson

Public static void main (string [] Arg) throws parseexception {
Boolean [] boolarray = new Boolean [] {true, false, true };
Jsonarray = jsonarray. fromobject (boolarray );
System. Out. println (jsonarray );
// Prints [true, false, true]
List list = new arraylist ();
List. Add ("first ");
List. Add ("second ");
Jsonarray jsonarray1 = jsonarray. fromobject (list );
System. Out. println (jsonarray1 );
// Prints ["first", "second"]
Jsonarray jsonarray2 = jsonarray. fromobject ("['json', 'is', 'easy']");
System. Out. println (jsonarray2 );
// Prints ["JSON", "is", "easy"]
Date date1 = new date ();
Simpledateformat Sf = new simpledateformat ("yyyy/mm/DD ");
String S = SF. Format (date1 );
Jsonarray jsonray1 = jsonarray. fromobject ("['" + S + "']");
System. Out. println (jsonray1.tostring ());
System. Out. println (jsonray1 );

// Date Processing

Eventdetails eventdetailitems = new eventdetails ();
Eventdetailitems. setdayid (1 );
Eventdetailitems. setdisplaykind (0 );
Eventdetailitems. seteventname ("SB ");
Date = new date ();
Eventdetailitems. setoccurrencedate (date );
Eventdetailitems. setrailroadxcode ("tbc03 ");
Eventdetailitems. setrailroadxname ("Japanese ");
Eventdetailitems. setinclueddate (new date ());
Eventdetailitems. setrowno (0 );
Jsonvalueprocessor jsonprocessor = new datejsonvalueprocessor ();
Jsonconfig = new jsonconfig ();
// Register a value Processor
Jsonconfig. registerjsonvalueprocessor (date. Class, jsonprocessor );
String json22 = jsonserializer. tojson (eventdetailitems, jsonconfig). tostring ();
System. Out. println (json22 );

Stbz0401updateoutputdata updates = new stbz0401updateoutputdata ();

List ssseventdetails = new arraylist <eventdetails> ();
For (INT I = 0; I <3; I ++ ){
Eventdetails eventdetailitems = new eventdetails ();
Eventdetailitems. setdayid (1 );
Eventdetailitems. setdisplaykind (0 );
Eventdetailitems. seteventname ("SB ");
Date = new date ();
String S1 = SF. Format (date );
Date = SF. parse (S1 );
Eventdetailitems. setoccurrencedate (date );
Eventdetailitems. setrailroadxcode ("tbc03 ");
Eventdetailitems. setrailroadxname ("Japanese ");
Eventdetailitems. setinclueddate (new date ());
Eventdetailitems. setrowno (0 );
Ssseventdetails. Add (eventdetailitems );
Updates. seteventdetailitems (ssseventdetails );
}
// Jsonarray JSON = jsonarray. fromobject (updates );
// System. Out. println (JSON. tostring ());
Jsonvalueprocessor jsonprocessor1 = new datejsonvalueprocessor ();
Jsonconfig jsonconfig1 = new jsonconfig ();
// Register a value Processor
Jsonconfig1.registerjsonvalueprocessor (date. Class, jsonprocessor1 );
String JSON = jsonserializer. tojson (updates, jsonconfig1). tostring ();
System. Out. println (JSON. tostring ());

}

 
}

Date Processing
Package com.nec.jp. railroadx. TBZ. tbz04;

Import java. Text. simpledateformat;
Import java. util. date;

Import net. SF. JSON. jsonconfig;
Import net. SF. JSON. processors. jsonvalueprocessor;

Class datejsonvalueprocessor implements jsonvalueprocessor {
Private string format = "yyyy-mm-dd ";

Public datejsonvalueprocessor (){
}

Public datejsonvalueprocessor (string format ){
This. format = format;
}

Public object processarrayvalue (object value, jsonconfig ){
String [] OBJ = {};
If (value instanceof date []) {
Simpledateformat Sf = new simpledateformat (format );
Date [] Dates = (date []) value;
OBJ = new string [dates. Length];
For (INT I = 0; I <dates. length; I ++ ){
OBJ [I] = SF. Format (dates [I]);
}
}
Return OBJ;
}

Public object processobjectvalue (string key, object value, jsonconfig ){
If (value instanceof date ){
String STR = new simpledateformat (Format). Format (date) value );
Return STR;
}
Return Value. tostring ();
}

Public String getformat (){
Return format;
}

Public void setformat (string format ){
This. format = format;
}

}

 

Bean class

Package com.nec.jp. railroadx. TBZ. tbz04;

Import java. util. List;

Import com.nec.jp. railroadx. TBZ. eventdetails;

/**
* Stbz0401updateaction outputs parameter values (JSON syntax and JavaScript syntax), and stbz0401logicbean. Update syntax.
* @ Author Wangjing
*/
Public class stbz0401updateoutputdata {

// Append an existing vertex to another vertex.
Private list <eventdetails> eventdetailitems;

/**
* @ Return the eventdetailitems
*/
Public list <eventdetails> geteventdetailitems (){
Return eventdetailitems;
}

/**
* @ Param eventdetailitems the eventdetailitems to set
*/
Public void seteventdetailitems (list <eventdetails> eventdetailitems ){
This. eventdetailitems = eventdetailitems;
}
 
}

 

// Bean class

Package com.nec.jp. railroadx. TBZ;

Import java. util. date;

Import org. Apache. struts2.json. Annotations. JSON;

/**
* Always keep always running
* @ Author Wangjing
*
*/
Public class eventdetails {

// Number
Private int rowno;
// Daily payment ID
Private int dayid;
// Birthday
Private date occurrencedate;
// Trusted day
Private date receiveddate;
// Step by step
Private string railroadxcode;
// Step-by-step name
Private string railroadxname;
// The name of the specified worker
Private string eventname;
// Each vertex has its own unique identifier.
Private int displaykind;
/**
* @ Return the rowno
*/
Public int getrowno (){
Return rowno;
}
/**
* @ Param rowno the rowno to set
*/
Public void setrowno (INT rowno ){
This. rowno = rowno;
}
/**
* @ Return the dayid
*/
Public int getdayid (){
Return dayid;
}
/**
* @ Param dayid the dayid to set
*/
Public void setdayid (INT dayid ){
This. dayid = dayid;
}
/**
* @ Return the occurrencedate
*/
@ JSON (format = "yyyy-mm-dd ")
Public date getoccurrencedate (){
Return occurrencedate;
}
/**
* @ Param occurrencedate the occurrencedate to set
*/
Public void setoccurrencedate (date occurrencedate ){
This. occurrencedate = occurrencedate;
}
/**
* @ Return the specified eddate
*/
@ JSON (format = "yyyy-mm-dd ")
Public date getreceiveddate (){
Return receiveddate;
}
/**
* @ Param receiveddate the specified eddate to set
*/
Public void setreceiveddate (date receiveddate ){
This. receiveddate = receiveddate;
}
/**
* @ Return the railroadxcode
*/
Public String getrailroadxcode (){
Return railroadxcode;
}
/**
* @ Param railroadxcode the railroadxcode to set
*/
Public void setrailroadxcode (string railroadxcode ){
This. railroadxcode = railroadxcode;
}
/**
* @ Return the railroadxname
*/
Public String getrailroadxname (){
Return railroadxname;
}
/**
* @ Param railroadxname the railroadxname to set
*/
Public void setrailroadxname (string railroadxname ){
This. railroadxname = railroadxname;
}
/**
* @ Return the eventname
*/
Public String geteventname (){
Return eventname;
}
/**
* @ Param eventname the eventname to set
*/
Public void seteventname (string eventname ){
This. eventname = eventname;
}
/**
* @ Return the displaykind
*/
Public int getdisplaykind (){
Return displaykind;
}
/**
* @ Param displaykind the displaykind to set
*/
Public void setdisplaykind (INT displaykind ){
This. displaykind = displaykind;
}
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.