Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Net.sf.json.JSONObject;
/** * Convert HashMap to JavaBean using JSON.
* @author Administrator
* @Date 2015-7-18 * *
public class People {
Private String namestring;
Private String agestring;
Public String toString () {
Return "[namestring=" +namestring+ "########" + "agestring=" +agestring+ "]";
}
Public String getnamestring () {return namestring;} public void setnamestring (String namestring) {this.namestring = NA mestring; } public String getagestring () {return agestring,} public void setagestring (String agestring) {this.agestring = Agest Ring }
public static void Main (string[] args) { map<string, string> map2 = new hashmap<string,string> (); String []strings = {"Namestring", "agestring"}; map<string, string> Map = new hashmap<string, string> (); map.put ("namestring", "Zhangsan"); map.put ("Agestring", "89"); map.put ("sexstring", "male"); Iterator<String> it = Map.keyset (). Iterator (); while (It.hasnext ()) { string keystring = It.next (); string valuestring = Map.get (keystring); for (String s:strings) { if (s==keystring) { map2.put (keystring,valuestring); } continue; } } jsonobject jsonobject = Jsonobject.fromobject (MAP2); people people = (people) Jsonobject.tobean (Jsonobject,People.class); system.out.println (People.tostring ()); } }
Java uses JSON to convert HashMap to JavaBean small example