json字符串转成 Map/List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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; /** * 说明 json字符串 转成 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" ); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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包环境下 遍历 不知道外层key的json字符串 */ public class JsonTest { public static void main(String[] args) { try { String json = "{\"1293142@1098820@1211867_1083940_1293396_1148650\":[{\"FS_ID\":\"1293396\",\"SX_MC\":\"李四\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"803932\"}],\"1293142@1098820@1211867_1083940_1293396_1097084\":[{\"FS_ID\":\"1293396\",\"SX_MC\":\"李四\",\"YFS_ID\":\"1293142@1084106@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"124\"}],\"1293142@1098820@1211867_1083940_1293331_1148650\":[{\"FS_ID\":\"1293331\",\"SX_MC\":\"张三\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"1005240\"}],\"1293142@1098820@1211867_1083940_1293331_1097084\":[{\"FS_ID\":\"1293331\",\"SX_MC\":\"张三\",\"YFS_ID\":\"1293142@1084106@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"204\"}],\"1293142@1098820@1211867_1083940_1284806_1148650\":[{\"FS_ID\":\"1284806\",\"SX_MC\":\"王小二\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"513475\"}],\"1293142@1098820@1211867_1083940_1284806_1097084\":[{\"FS_ID\":\"1284806\",\"SX_MC\":\"王小二\",\"YFS_ID\":\"1293142@1084106@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"145\"}],\"1293142@1098820@1211867_1083940_1293461_1148650\":[{\"FS_ID\":\"1293461\",\"SX_MC\":\"朱重八\",\"YFS_ID\":\"1293142\",\"SZ_ID\":\"1148650\",\"SX_Z\":\"339040\"}],\"1293142@1098820@1211867_1083940_1293461_1097084\":[{\"FS_ID\":\"1293461\",\"SX_MC\":\"朱重八\",\"YFS_ID\":\"1293142@1084106@1103027\",\"SZ_ID\":\"1097084\",\"SX_Z\":\"52\"}]}" ; JSONObject jsonObj = new JSONObject(json); //方式二 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" ); //方式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(); } } } |
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
2011-08-21 flash chart(amCharts的破解)