Convert various types of data into JSON and fetch data from JSON in Java

Source: Internet
Author: User

First, import the following jar packages


Here are the various types of programs that convert to JSON and extract data from JSON

Package Jsontest;import Java.util.arraylist;import java.util.hashmap;import java.util.list;import java.util.Map; Import Net.sf.json.jsonarray;import Net.sf.json.jsonobject;public class Jsonmain {public static void main (string[] args ) {GetList (setlist ()); Getmap (Setmap ()); System.out.println ("Other similar---------------------"); System.out.println ("Convert object to JSON-------------------"); System.out.println (Setbean ()); System.out.println ("Convert array to JSON-------------------"); System.out.println (Setshuzu ()); System.out.println ("Convert object collection to JSON-----------------"); System.out.println (Setlistbeans ());} /** * Create a list array and convert it to JSON data * @return */public static String setlist () {list<string> list = new arraylist<string& gt; (); List.add ("one"), List.add ("one"), List.add ("three"); List.add ("four"); Jsonarray ja = jsonarray.fromobject (list); return ja.tostring ();}  /** * Create a map array and convert it to JSON data * @return */public static String setmap () {map<string,string> map =new hashmap<string, String> (); Map.put ("Name", "Xiaozhao"); map.Put ("Sex", "man"); Map.put ("Address", "China"); Jsonobject JB = jsonobject.fromobject (map); return jb.tostring ();} /** * Create an object and convert it to JSON data * @return */public static String Setbean () {UserBean UB = new UserBean (); Ub.setusername ("Zd"); ub.se Tpassword ("Zd"); ub.setage; ub.setaddress ("Henan"); Jsonobject JB = jsonobject.fromobject (UB); return jb.tostring ();} /** * Create an array and convert it to JSON data * @return */public static String Setshuzu () {Object [] Shuzu = {"One", 23,true, "four"}; Jsonarray JB = jsonarray.fromobject (Shuzu); return jb.tostring ();} /** * Create a list of objects and convert them to JSON data * @return */public static String Setlistbeans () {list<userbean> list = new Arraylist&lt ; Userbean> (); UserBean ub1 = new UserBean (), Ub1.setusername ("zz"), Ub1.setpassword ("zz"), ub1.setage (+); Ub1.setaddress ("Jinan"); UserBean ub2 = new UserBean () ub2.setusername ("DD"); Ub2.setpassword ("DD"); Ub2.setage ("ub2.setaddress"); List.add (UB1); List.add (UB2); Jsonarray ja2 = jsonarray.fromobject (list); return ja2.tostring ();} /** * Convert JSON data to LiSt * @param list */public static void GetList (String list) {System.out.println ("list converted to JSON data----------------------"); SYSTEM.OUT.PRINTLN (list); Jsonarray js = jsonarray.fromobject (list); System.out.println ("Convert JSON data to List---------------------"), for (int i =0; i<js.size (); i++) {System.out.println ( Js.get (i));}} /** * Remove data from map into JSON * @param map */public static void Getmap (String map) {System.out.println (" Map into JSON data-----------------------"); SYSTEM.OUT.PRINTLN (map); System.out.println ("Remove the data from the map into JSON---------------"); Jsonobject JB = jsonobject.fromobject (map); System.out.println (Jb.get ("Sex")); System.out.println (Jb.get ("Address")); System.out.println (Jb.get ("name"));}}
Where Userbean is an ordinary entity class

Here is the result of the run



Convert various types of data into JSON and fetch data from JSON in Java

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.