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