JSONObject jsonObject = new JSONObject(jsonString); Iterator iterator = jsonObject.keys(); while(iterator.hasNext()){ key = (String) iterator.next(); value = jsonObject.getString(key); }
private static Map<String, List<NewNet>> toMap(Object object) { Map<String, List<NewNet>> data = new HashMap<String, List<NewNet>>(); JSONObject jsonObject = JSONObject.fromObject(object); Iterator ite = jsonObject.keys(); while (ite.hasNext()) { String key = String.valueOf(ite.next()); JSONArray obj = JSONArray.fromObject(jsonObject.get(String.valueOf(key))); Iterator<Object> it = obj.iterator(); while (it.hasNext()) { JSONObject Obj= JSONObject.fromObject(it.next()); NewNet value = (NewNet) Obj.toBean(Obj,NewNet.class); if(data.keySet().contains(key)){ data.get(key).add(value); }else{ List<NewNet> List=new ArrayList<NewNet>(); List.add(value); data.put(key, List); } } } return data; }
public static List<RPingCheckParamsDto> jsonToVar(String jsons){ JSONArray objs = JSONArray.fromObject(jsons); Iterator<JSONObject> it = objs.iterator(); List<RPingCheckParamsDto> dtos = new ArrayList<RPingCheckParamsDto>(); while (it.hasNext()) { JSONObject ob = (JSONObject) it.next(); RPingCheckParamsDto dto = new RPingCheckParamsDto(); if(ob.getString("device")!=null){ dto.setDevice((Device)JSONObject.toBean(JSONObject.fromObject(ob. get("device")),Device.class)); } if(ob.getString("starttime")!=null){ dto.setStarttime((Long)ob.get("starttime")); } if(ob.getString("type")!=null){ dto.setType((Integer)ob.get("type")); } if(ob.getString("segs")!=null){ dto.setSegs((ArrayList<IPTreeNodeBean>)JSONArray.toList(JSONArray. fromObject(ob.get("segs")),IPTreeNodeBean.class)); } if(ob.getString("assignedIps")!=null){ ArrayList<String> Ps = (ArrayList<String>)JSONArray.toCollection(JSONArray. fromObject(ob.get("assignedIps"))); Set<String> bs = new HashSet<String>(); for(String p:Ps){ bs.add(p); } if(ob.getString("nsegs")!=null){ dto.setNsegs((ArrayList<NewNet>)JSONArray.toList(JSONArray. fromObject(ob.get("nsegs")),NewNet.class)); } dto.setAssignedIps(bs); } if(dto!=null){ dtos.add(dto); } } return dtos; }
在信息解析的过程中发生很多bug ,直接gson去转,这里很是推荐这个,很好用,自定义类型的转化效率很高