Convert java json string to Map or List, jsonmap
Import java. util. list; import java. util. map; import java. util. map. entry; import net. sf. json. JSONArray; import net. sf. json. JSONObject; /*** converts the json String to Map/List * @ author xss * @ date 2013-1-18 10:22:41 * @ mail wuniu2010@126.com */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 ");}}}}
Org. json jar package environment: (the jar package required by json: Click here to download)
Import java. util. iterator; import java. util. set; import org. json. JSONArray; import org. json. JSONException; import org. json. JSONObject;/*** @ date 2013-2-1 10:36:22 * @ author xss * @ org. json jar package environment traversal does not know the json String of the outer key */public class JsonTest {public static void main (String [] args) {try {String json = "{\" 1293142 @ 1098820 @ 1211867_00003940_12930000_1148650 \ ": [{\" FS_ID \ ": \" 1293396 \ ", \" SX_MC \": \ "Li Si \", \ "YFS_ID \": \ "1293142 \", \ "SZ_ID \": \ "1148650 \", \ "SX_Z \": \ "803932 \"}], \ "1293142 @ 1098820 @ 1211867_00003940_12930000_00007084 \": [{\ "FS_ID \": \ "1293396 \", \ "SX_MC \": \ "Li Si \", \ "YFS_ID \": \ "1293142 @ 1084106 @ 1103027 \", \ "SZ_ID \": \ "1097084 \", \ "SX_Z \": \ "124 \"}], \ "1293142 @ 1098820 @ 1211867_00003940_1293331_1148650 \": [{\ "FS_ID \": \ "1293331 \", \ "SX_MC \": \ "Zhang San \", \ "YFS_ID \": \ "1293142 \", \ "SZ_ID \": \ "1148650 \", \ "SX_Z \": \ "1005240 \"}], \ "1293142 @ 1098820 @ 1211867_00003940_1293331_00007084 \": [{\ "FS_ID \": \ "1293331 \", \ "SX_MC \": \ "Zhang San \", \ "YFS_ID \": \ "1293142 @ 1084106 @ 1103027 \", \ "SZ_ID \": \ "1097084 \", \ "SX_Z \": \ "204 \"}], \ "1293142 @ 1098820 @ 1211867_00003940_1284806_1148650 \": [{\ "FS_ID \": \ "1284806 \", \ "SX_MC \": \ "Wang xiao'er \", \ "YFS_ID \": \ "1293142 \", \ "SZ_ID \": \ "1148650 \", \ "SX_Z \": \ "513475 \"}], \ "1293142 @ 1098820 @ 1211867_00003940_1284806_00007084 \": [{\ "FS_ID \": \ "1284806 \", \ "SX_MC \": \ "Wang xiao'er \", \ "YFS_ID \": \ "1293142 @ 1084106 @ 1103027 \", \ "SZ_ID \": \ "1097084 \", \ "SX_Z \": \ "145 \"}], \ "1293142 @ 1098820 @ 1211867_00003940_129000000001148650 \": [{\ "FS_ID \": \ "1293461 \", \ "SX_MC \": \ "Zhu Zhongba \", \ "YFS_ID \": \ "1293142 \", \ "SZ_ID \": \ "1148650 \", \ "SX_Z \": \ "339040 \"}], \ "1293142 @ 1098820 @ 1211867_00003940_129000000007084 \": [{\ "FS_ID \": \ "1293461 \", \ "SX_MC \": \ "Zhu Zhongba \", \ "YFS_ID \": \ "1293142 @ 1084106 @ 1103027 \", \ "SZ_ID \": \ "1097084 \", \ "SX_Z \": \ "52 \"}]} "; JSONObject jsonObj = new JSONObject (json); // method 2 Iterator it = jsonObj. keys (); for (int j = 0; it. hasNext (); j ++) {String str = it. next (). toString (); System. out. println (str); JSONArray array = new JSONArray (jsonObj. get (str ). toString (); System. out. println (array); for (int I = 0; I <array. length (); I ++) {JSONObject jons = array. getJSONObject (I); System. out. println (jons. get ("SX_MC") ;}} System. out. println ("tep2"); // method 1 Set <String> set = jsonObj. keySet (); for (String str: set) {System. out. println (str); JSONArray array = new JSONArray (jsonObj. get (str ). toString (); System. out. println (array); for (int I = 0; I <array. length (); I ++) {JSONObject jons = array. getJSONObject (I); System. out. println (jons. get ("SX_MC") ;}} catch (JSONException e) {e. printStackTrace ();}}}
Published on Iteye