Convert Java object to JSON format

Source: Internet
Author: User

Use JSON-lib to convert a Java object to a JSON string

After learning to use Ajax in J2EE, sometimes the client data returned from the server is an object. It is easy to make errors when you manually piece together the JSON string. Google knows the JSON-lib class library, it is convenient to support conversion from Java objects to JSON strings.
See the JSON-lib introduction of source-Forge.
Jakarta commons-lang 2.4
Jakarta commons-beanutils 1.7.0
Jakarta commons-collections 3.2
Jakarta commons-logging 1.1.1
Ezmorph 1.0.6

The support for these jar packages has been downloaded and tried, which is quite useful.CodeThere are three cases: 1 object 2 List 3 object and list object)

1 object:

package COM. JSON. action. main; </P> <p> Import Java. text. simpledateformat; </P> <p> Import net. SF. JSON. jsonobject; </P> <p> Import COM. JSON. model. usermodel; </P> <p> public class userobjectjsonmain {</P> <p> Public static void main (string [] ARGs) {</P> <p> simpledateformat SDF = new simpledateformat ("Yyyy-mm-dd hh: mm: SS"); </P> <p> usermodel UA = new usermodel (); <br/> UA. setid (11); <br/> UA. settitle ("JSON transmission"); <br/> UA. setcontent ("Convert Java object to JSON file format! "); <Br/> UA. setcurrent (SDF. format (New Java. util. date (); <br/> jsonobject = jsonobject. fromobject (UA); <br/> system. out. println ("Java object to JSON:" + jsonobject ); <br/>}</P> <p >}< br/>

2 list

Package COM. JSON. action. main; </P> <p> Import Java. text. simpledateformat; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; <br/> Import net. SF. JSON. jsonarray; </P> <p> Import COM. JSON. model. usermodel; </P> <p> public class userlistjsonmain {</P> <p> Public static void main (string [] ARGs) {<br/> simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); </P> <p> List <usermodel> List = new ARRA Ylist <usermodel> (); <br/> for (INT I = 1; I <10; I ++) {<br/> usermodel user = new usermodel (); <br/> User. setid (I); <br/> User. settitle ("JSON transmission" + I); <br/> User. setcontent ("Convert Java object to JSON file format! "+ I); <br/> User. setcurrent (SDF. format (New Java. util. date (); <br/> list. add (User); <br/>}</P> <p> jsonarray = jsonarray. fromobject (list); <br/> system. out. println (jsonarray); </P> <p >}< br/>

3 object and list

Package COM. JSON. action. main; </P> <p> Import Java. text. simpledateformat; <br/> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. list; <br/> Import Java. util. map; </P> <p> Import net. SF. JSON. jsonobject; </P> <p> Import COM. JSON. model. usermodel; </P> <p> public class userlistobjectjsonmain {</P> <p> Public static void main (string [] ARGs) {<br/> simpledateformat SDF = new simpledateform At ("yyyy-mm-dd hh: mm: SS"); </P> <p> usermodel UA = new usermodel (); <br/> UA. setid (11); <br/> UA. settitle ("JSON transmission"); <br/> UA. setcontent ("Convert Java object to JSON file format! "); <Br/> UA. setcurrent (SDF. format (New Java. util. date (); </P> <p> List <usermodel> List = new arraylist <usermodel> (); <br/> for (INT I = 1; I <10; I ++) {<br/> usermodel user = new usermodel (); <br/> User. setid (I); <br/> User. settitle ("JSON transmission" + I); <br/> User. setcontent ("Convert Java object to JSON file format! "+ I); <br/> User. setcurrent (SDF. format (New Java. util. date (); <br/> list. add (User); <br/>}</P> <p> // when both objects and arrays exist <br/> Map <string, object> map = new hashmap <string, Object> (); <br/> map. put ("UA", UA); <br/> map. put ("lis", list); </P> <p> jsonobject jsonobjectfrommap = jsonobject. fromobject (MAP); <br/> system. out. println (jsonobjectfrommap); </P> <p >}< br/>

For personal study, share with the university!

Related Article

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.