1. If the conversion is a map, or a Simple object
Package com.gc.action;Import Java.util.Map;Import Net.sf.json.JSONObject;/** * 1.json string converted to map * 2.json string to Object *@author ZHANGTENGDA * * * *PublicClassjsonutils {PublicStaticvoidMain (string[] args) {String jsonstr ="{\" status\ ": \" 0\ ", \" data\ ": {\" name\ ": \" tom\ ", \" age\ ": 18}}";// ================================1. If you are taking out the outermost map map<object, object> result = Jsontomap (JSONSTR); System.out.println (Result.get ("status")); System.out.println (Result.get ("Data"));2. If progress one gets to the inner layer of map map<object, object> data = Jsontomap (Result.get ("Data")); System.out.println (Data.get ("Name")); System.out.println (Data.get ("Age"));================================ Long beginTime = System.currenttimemillis ();3. Convert to object String personstr ="{\" id\ ": 12,\" name\ ": \" Mary\ "}"; Person person = Jsontobean (personstr, Person.class); SYSTEM.OUT.PRINTLN (person); System.out.println ("Time-consuming:" + (System.currenttimemillis ()-beginTime)); }/** * JSON string converted to map object *@param jsonobj * @return * /public static map<object, object> jsontomap (Object jsonobj) {Jsono Bject jsonobject = Jsonobject.fromobject (jsonobj); Map<object, object> map = (map) jsonobject; return map;} /**json String to Object * @param jsonobj * @param type * @return */public static <T> T
jsontobean (Object jsonobj, class<t> type) {Jsonobject jsonobject = Jsonobject.fromobject (jsonobj); T obj = (t) Jsonobject.tobean (jsonobject, type);
return obj;} }
JSON into map