Using Fastjson to parse data in HashMap

Source: Internet
Author: User

ImportJava.util.HashMap; ImportJava.util.Iterator; ImportJava.util.Map; ImportJava.util.Map.Entry; ImportJava.util.Set; ImportCom.alibaba.fastjson.JSON; Importcom.alibaba.fastjson.TypeReference;  Public classDemo { Public Static voidMain (string[] args) {Map<string, student> map =NewHashmap<string, student>(); Student STU1=NewStudent ("1", "Sharapova"); Student STU2=NewStudent ("2", "Bouchard"); Student STU3=NewStudent ("3", "Halep"); Map.put ("00011", STU1); Map.put ("00012", STU2); Map.put ("00013", STU3);          OBJECTTOSTR (map); String Str= "{' {' 00011 ': {' id ' = ' ': ' 1 ', ' name ': ' Silei '}, ' 00012 ': {' id ': ' 2 ', ' name ': ' Bouchard '}, ' 00013 ': {' id ': ' 3 ', ' name ': ' Halep '}}";      Strtoobject (str); }           Public Static voidstrtoobject (String str) {Map<string, student> map = (map<string, student>) json.parseobject (str,NewTypereference<map<string, student>>() {          }); Set<entry<string, student>> m =Map.entryset (); Iterator<entry<string, student>> it =M.iterator ();  while(It.hasnext ()) {Entry<string, student> en =It.next (); String ID=En.getkey (); Student Stu=En.getvalue (); System.out.println (Stu.getid ()+ "===" +stu.getname ());      } System.out.println (Map.size ()); }        //convert an object to a JSON string     Public Static voidobjecttostr (map map) {String str=json.tojsonstring (map);      System.out.println (str); }  }  

Using Fastjson to parse data in HashMap

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.