Json-lib Foundation

Source: Internet
Author: User

One, json-lib the required jar package:

  Json-lib.jar,Commons-beanutils.jar,commons-collections.jar,commons-lang.jar,commons-logging.jar, Ezmorph-1.0.6.jar (can search for downloads directly online).

Second, use:

1, JSON string   <------>  JSON

String jsonstr= "{\" name\ ": \" Peter\ ", \" age\ ": 25,\" address\ ": \" guangzhou \ ", \" weight\ ": \" 55.5\ ", \" handsome\ ": true}";
//String------>json Jsonobject jsonobject=jsonobject.fromobject (JSONSTR);
JSON------>string
String jsonstring=jsonobject.tostring ();

  2. Java entity class <------> JSON

 public  class   User { private      String name;  private  int      age;     private   String address;  private  double   weight;  private  boolean   handsome;  //  omit construction and Get/set method } 
User user=Newtrue);
  //java entity class------>json Jsonobject jsonobject=jsonobject.fromobject (user);

  
   // {"Weight": 55.5, "Address": "Guangzhou", "name": "Peter", "Age": $, "Handsome": true} System.out.println (Jsonobject);

  3. List <------> JSON

        list<string> list=New arraylist<string>();        List.add ("QQQQQQ");        List.add ("wwwwww");        List.add ("eeeeee");        List.add ("rrrrrr");         // List------>json        Jsonarray jsonarray=jsonarray.fromobject (list);          // JSON------>list        List<string> list3= (List) jsonarray.tocollection (Jsonarray);

  4. Map <------> JSON

Map<string, string> map=new hashmap<string, string>();        Map.put ("name", "Peter");        Map.put ("Sex", "man");        Map.put ("Age", "a");         // Map------JSON        Jsonobject jsonmap=jsonobject.fromobject (map);         // JSON------>map        Map<string, string> mapjson= (map) Jsonobject.tobean (jsonmap, map.  Class);

 5. Set <------> JSON

Set<string> set=NewHashset<string>(); Set.add ("QQQQQQ"); Set.add ("Wwwwww"); Set.add ("Eeeeee"); //Set------>jsonJsonarray array=Jsonarray.fromobject (set); //JSON------>setSet<string> set1=NewHashset<string>();  for(Object object:array) {Set1.add (object.tostring ()); }                //Error: Java.lang.ClassCastExceptionSet<string> setjson= (set<string>) jsonarray.tocollection (Array,set.class); //go directly to listlist<string> list= (list) jsonarray.tocollection (array);

  

Json-lib Foundation

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.