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 58 59 60
| public static String serializeObjectToJSON(Object obj) { if (obj == null) return null; else { JSONObject jsonObject = JSONObject .fromObject(obj); return jsonObject.toString(); } }
@SuppressWarnings("unchecked") public static Object deserializeJSONToObject( String json, Class rootClass, Map classMapping) { JSONObject jsonObject = JSONObject.fromObject(json); if (classMapping != null) return JSONObject.toBean( jsonObject, rootClass, classMapping); else return JSONObject.toBean(jsonObject, rootClass); }
@SuppressWarnings("unchecked") public static Map deserializeJSONToMap(String json) { JSONObject jsonObject = JSONObject.fromObject(json); Iterator<?> it = jsonObject.keys(); Map map = null; while (it.hasNext()) { Object key = it.next(); Object o = jsonObject.get(key); if(map==null)map = new HashMap(); map.put(key, o); } return map; } @SuppressWarnings("unchecked") public static void main(String[] args) { String s = "{country:\"EP\",appNumber:\"dse232wewe\", eesNumber:\"1231331\", ipType:\"1\", eesId:\"323232\", eesDate:\"2012-03-23\",filingDate:\"2012-03-23\"," + "appDate:\"2012-03-23\"," + "applicantName:[{prifex:\"f1\",lastName:\"ln\",firstName:\"fn\",middleName:\"mn\",suffix:\"sf\"},{prifex:\"f2\",lastName:\"ln\",firstName:\"fn\",middleName:\"mn\",suffix:\"sf\"}]" + " }";
Map m =deserializeJSONToMap(s);
List applicantMap = (List)m.get("applicantName"); System.out.println(applicantMap); if(applicantMap!=null&&applicantMap instanceof List){ Iterator iterator = applicantMap.iterator(); while(iterator.hasNext()) { Map v = (Map)iterator.next(); System.out.println(v.get("prifex")); } } }
|