Use JSON-lib to generate JSON text

Source: Internet
Author: User

JSON-lib test class:

Package COM. jadyer. JSON; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. hashset; <br/> Import Java. util. list; <br/> Import Java. util. map; <br/> Import Java. util. set; </P> <p> Import net. SF. JSON. jsonarray; <br/> Import net. SF. JSON. jsonobject; </P> <p> Import COM. jadyer. model. student; <br/> Import COM. jadyer. model. teacher; </P> <p>/** <br/> * Java's support for JSON is not only reflected in JSON-lib, for more information, visit http://www.json.org <br/> * @ See also has a more powerful toolkit Jackson, its official website is http://jackson.codehaus.org <br/> * @ see has been tested on the internet, get the conclusion Jackson> gson> JSON-lib, for more information, see http://wangym.javaeye.com/blog/738933 <br/> * @ see JSON-lib to convert entity, MAP, collection, array, and XML in Java into JSON data formats <br/> * @ See note json-lib-2.4-jdk15.jar also dependent on other jar packages <br/> * @ see depends on ezmorph-1.0.6, commons-lang-2.5, commons-beanutils-1.8.0, commons-collections3.2.1, commons-logging-1.1.1 <br/> * @ See Official JSON-lib the website is http://json-lib.sourceforge.net <br/> * @ see ezmorph official website is http://ezmorph.sourceforge.net <br/> */<br/> public class jsonlibdemo {<br/> Public static void main (string [] ARGs) {<br/> testjavabean (); <br/> testmap (); <br/> testarray (); <br/>}</P> <p> Public static void testjavabean () {<br/> Student Stu = new student ("Shen Lang", 22 ); <br/> jsonobject = jsonobject. fromobject (Stu); <br/> system. out. println (jsonobject. tostring (); </P> <p> Teacher = new teacher (); <br/> teacher. setname ("Zhang qiling"); <br/> teacher. getstudents (). add (new student ("Wu xie", 25); <br/> teacher. getstudents (). add (new student ("black glasses", 26); <br/> teacher. getstudents (). add (new student ("Chen Wenjin", 27); <br/> jsonobject jsonobject22 = jsonobject. fromobject (teacher); <br/> system. out. println (jsonobject22.tostring (); <br/>}</P> <p> Public static void testmap () {<br/> Map <string, object> map = new hashmap <string, Object> (); <br/> map. put ("name", "Oil Bottle"); <br/> map. put ("favorate", new string [] {"sleeping", "Daze", "cool"}); <br/> map. put ("getfavorate", "function (I) {return this. favorate [I]} "); <br/> jsonobject = jsonobject. fromobject (MAP); <br/> system. out. println (jsonobject); <br/>}</P> <p> Public static void testarray () {<br/> string [] users = {"Zhao hongbing ", "Little Beijing", "dongba Tian" };< br/> jsonarray = jsonarray. fromobject (users); <br/> system. out. println (jsonarray. tostring (); </P> <p> integer [] numbers = {1, 2, 3}; <br/> jsonarray = jsonarray. fromobject (numbers); <br/> system. out. println (jsonarray. tostring (); </P> <p> Boolean [] booleanarray = {true, false, false, true}; <br/> jsonarray = jsonarray. fromobject (booleanarray); <br/> system. out. println (jsonarray. tostring (); </P> <p> List <string> List = new arraylist <string> (); <br/> list. add ("Zhang Xiaofan"); <br/> list. add ("Lin Jing Yu"); <br/> list. add ("tens of thousands"); <br/> jsonarray = jsonarray. fromobject (list); <br/> system. out. println (jsonarray. tostring (); </P> <p> set <student> students = new hashset <student> (); <br/> students. add (new student ("Lu Yi Zhi", 25); <br/> students. add (new student ("Yunlong", 26); <br/> students. add (new student ("Jin Yufeng", 27); <br/> jsonarray = jsonarray. fromobject (students); <br/> system. out. println (jsonarray. tostring (); <br/>}< br/>}

 

Entity classes used

Package COM. jadyer. model; <br/> public class student {<br/> private string name; <br/> private integer age; <br/>/* getter and setter methods for the two attributes */<br/> Public student () {}< br/> Public student (string name, integer age) {<br/> This. name = Name; <br/> This. age = age; <br/>}</P> <p> package COM. jadyer. model; <br/> Import Java. util. hashset; <br/> Import Java. util. set; <br/> public class teacher {<br/> private string name; <br/> private set <student> students = new hashset <student> (); <br/>/* The getter and setter methods of the two attributes are omitted */<br/>}

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.