In the process of reading any questions, welcome to communicate together
Email: [email protected]
QQ:1494713801
Java code
- Package Jsontomap;
- Import java.util.List;
- Import Java.util.Map;
- Import Java.util.Map.Entry;
- Import Net.sf.json.JSONArray;
- Import Net.sf.json.JSONObject;
- /**
- * Description JSON string converted to Map/list
- * @author XSS
- * @date 2013-1-18 10:22:41
- * @mail [email protected]
- */
- Public class jsontomap {
- Public static void main (string[] args) {
- //jsonarray
- String jsonarraydata="[{\" a1\ ": \" 12\ ", \" b1\ ": \" 112\ ", \" c1\ ": \" 132\ ", \" d1\ ": \" 134\ "},{\" a2\ ": \" 12\ ", \" b2\ ": \ "112\", \ "c2\": \ "132\", \ "d2\": \ "134\"},{\ "a3\": \ "12\", \ "b3\": \ "112\", \ "c3\": \ "132\", \ "d3\": \ "134\"}] ";
- Jsonarray Jsonarray = Jsonarray.fromobject (Jsonarraydata);
- List<map<string,object>> Maplistjson = (List) Jsonarray;
- for (int i = 0; i < maplistjson.size (); i++) {
- Map<string,object> Obj=maplistjson.get (i);
- for (entry<string,object> entry:obj.entrySet ()) {
- String strkey1 = Entry.getkey ();
- Object Strval1 = Entry.getvalue ();
- System.out.println ("KEY:"+strkey1+"-Value:"+strval1+"\ n" );
- }
- }
- //Jsonobject
- String jsonobjectdata="{\" data1\ ": {\" a1\ ": \" 12\ ", \" b1\ ": \" 112\ ", \" c1\ ": \" 132\ ", \" d1\ ": \" 134\ "},\" data2\ ": {\ "a2\": \ "12\", \ "b2\": \ "112\", \ "c2\": \ "132\", "d2\": \ "134\"},\ "data3\": {\ "a3\": \ "12\", \ "b3\": \ "112\", \ "c3\": \ " 132\ ", \" d3\ ": \" 134\ "}}";
- Jsonobject jsonobject = Jsonobject.fromobject (Jsonobjectdata);
- map<string, object> Mapjson = Jsonobject.fromobject (Jsonobject);
- for (entry<string,object> entry:mapJson.entrySet ()) {
- Object Strval1 = Entry.getvalue ();
- Jsonobject JsonObjectStrval1 = Jsonobject.fromobject (STRVAL1);
- map<string, object> mapJsonObjectStrval1 = Jsonobject.fromobject (JSONOBJECTSTRVAL1);
- System.out.println ("KEY:"+entry.getkey () +"-Value:"+entry.getvalue () + "\ n" );
- for (entry<string, object> entry1:mapJsonObjectStrval1.entrySet ()) {
- System.out.println ("KEY:"+entry1.getkey () +"-Value:"+entry1.getvalue () + "\ n");
- }
- }
- }
- }
Reference Link: http://wuniu2010.iteye.com/blog/1771934
"Deep Java" converts a JSON string into map/list