Using Java code to complete the conversion to JSON format

Source: Internet
Author: User

 

/** *  */Package Net.nyist.jsondemo.servlet;import java.util.arraylist;import java.util.hashmap;import java.util.List; Import Java.util.map;import org.json.JSONObject;/** * @author Yuchao * * @school the application and development of the mobile device of Nanyang Institute of Technology software 11 soft Mobile Four Classes * * @time 2014-9-17 pm 9:24:29*/ Public classJsondemo { Public Static voidMain (string[] args) {Map<string, object> map =NewHashmap<string, object>(); List<map<string, object>> list =NewArraylist<map<string, object>>(); Map.put (" Age", A); Map.put ("name","Yuchao");         List.add (map); Map=NewHashmap<string, object>(); Map.put (" Age", +); Map.put ("name","Zhangsan");         List.add (map); Map=NewHashmap<string, object>(); Map.put ("Users", list); Jsonobject Jsonobject=Newjsonobject (map); System. out. println (Jsonobject.tostring ()); }}
/** *  */Package Net.nyist.jsondemo.entity;import java.io.Serializable;/** * @author Yuchao * * @school the application and development of the mobile device of Nanyang Institute of Technology software 11 soft Mobile Four Classes * * @time 2014-9-17 pm 11:53:32*/@SuppressWarnings ("Serial") Public classAddress implements serializable{PrivateString Codenumber; PrivateString City;  PublicString Getcodenumber () {returnCodenumber; }     Public voidSetcodenumber (String codenumber) { This. Codenumber =Codenumber; }     PublicString getcity () {returnCity ; }     Public voidsetcity (String city) { This. City =City ; }    }/** *  */Package Net.nyist.jsondemo.entity;import java.io.Serializable;/** * @author Yuchao * * @school the application and development of the mobile device of Nanyang Institute of Technology software 11 soft Mobile Four Classes * * @time 2014-9-17 pm 11:54:43*/@SuppressWarnings ("Serial") Public classUser implements serializable{Private intID; PrivateString name; Private intAge ; Privateaddress address;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicAddress getaddress () {returnaddress; }     Public voidsetaddress (address address) { This. Address =address; }}/** *  */Package Net.nyist.jsondemo.servlet;import java.util.arraylist;import java.util.hashmap;import java.util.List; Import Java.util.map;import Net.nyist.jsondemo.entity.address;import Net.nyist.jsondemo.entity.user;import Org.json.JSONArray;/** * @author Yuchao * * @school the application and development of the mobile device of Nanyang Institute of Technology software 11 soft Mobile Four Classes * * @time 2014-9-17 pm 11:53:03*/ Public classJsonobjectdemo { Public Static voidMain (string[] args) {address address=NewAddress (); Address.setcity ("Henan Nanyang"); Address.setcodenumber ("473000"); User User=NewUser (); User.setid (1); User.setname ("Yuchao");           User.setaddress (address); Map<string, object> map =NewHashmap<string, object>(); List<User> list =NewArraylist<user>();           List.add (user); Address=NewAddress (); Address.setcity ("Zhengzhou, Henan"); Address.setcodenumber ("473000"); User=NewUser (); User.setid (2); User.setname ("liling");           User.setaddress (address);           List.add (user); Map.put ("Users", list); Jsonarray Jsonarray=NewJsonarray ();           Jsonarray.put (map); System. out. println (Jsonarray.tostring (3)); } }

Using Java code to complete the conversion to JSON format

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.