String str = "{'status':1,'message':'制作证书成功','data':{'userId':'','buf':'','bufP7':'','certDn':'','certSn':'','issuerDn':'','startTime':'','endTime':''}";

JSONObject j = new JSONObject();

HashMap<String, String> map = new HashMap<String, String>();

map.put("map1", "map1");

map.put("map2", "map2");

map.put("map3", "map3");

j.put("files", map);

j.put("test", "test");

j.put("test1", "test1");

System.out.println(j.toString());

String str1 = j.toString();

JSONObject json = JSONObject.fromObject(str1);

System.out.println(json.get("files"));

JSONObject j2 = JSONObject.fromObject(json.get("files"));

System.out.println(j2.get("map1"));

输出结果:


{"files":{"map3":"map3","map2":"map2","map1":"map1"},"test":"test","test1":"test1"}

{"map3":"map3","map2":"map2","map1":"map1"}

map1




net.sf.json.JSONObject json = new  net.sf.json.JSONObject();

List<Contract4RealInfo> list4RealInfo = new ArrayList<Contract4RealInfo>();

HashMap<String, Contract4RealInfo> mHashMap = new HashMap<String, Contract4RealInfo>();

for (int i = 0; i < list.size(); i++) {

Contract4RealInfo contract4RealInfo = new Contract4RealInfo();

Contract contract = list.get(i);

contract4RealInfo.setContractid(contract.getId());

contract4RealInfo.setDate_created(contract.getDateCreated());

contract4RealInfo.setDoc_num(contract.getDocNum());

contract4RealInfo

.setRealinfo_order(contract.getRealInfoOrder());

contract4RealInfo.setRealinfo_uid(contract.getRealInfoUid());

list4RealInfo.add(contract4RealInfo);

mHashMap.put(contract.getId()+"", contract4RealInfo);

}

json.put("infos", mHashMap);


logger.info("===>>>getContractInfo:" + json.toString());

return json.toString();


输出:

{"infos":{"3":{"contractid":3,"date_created":"2016-06-02 21:58:05","doc_num":"e490086e053d4a77b37a83ae5de56ec0","realinfo_order":"","realinfo_uid":"","title":""},"10":{"contractid":10,"date_created":"2016-06-03 08:55:54","doc_num":"TEST2016060300002","realinfo_order":"","realinfo_uid":"","title":""},"7":{"contractid":7,"date_created":"2016-06-03 08:50:39","doc_num":"-8261928884766610432","realinfo_order":"","realinfo_uid":"","title":""},"6":{"contractid":6,"date_created":"2016-06-03 08:50:17","doc_num":"8391885196411773952","realinfo_order":"","realinfo_uid":"","title":""},"5":{"contractid":5,"date_created":"2016-06-03 08:49:41","doc_num":"-5565323376995741696","realinfo_order":"","realinfo_uid":"","title":""},"4":{"contractid":4,"date_created":"2016-06-03 08:49:40","doc_num":"-5565323378640957440","realinfo_order":"","realinfo_uid":"","title":""},"9":{"contractid":9,"date_created":"2016-06-03 08:54:19","doc_num":"TEST2016060300001","realinfo_order":"","realinfo_uid":"","title":""},"8":{"contractid":8,"date_created":"2016-06-03 08:50:40","doc_num":"-5565323377500368896","realinfo_order":"","realinfo_uid":"","title":""}}}




public static void main(String[] args) {

List<Contract4RealInfo> l = new ArrayList<Contract4RealInfo>();

Contract4RealInfo c = new Contract4RealInfo();

c.setContractid(2L);

Contract4RealInfo c1 = new Contract4RealInfo();

c1.setContractid(3L);

l.add(c);

l.add(c1);

net.sf.json.JSONObject json = new net.sf.json.JSONObject();


json.put("infos", l);

System.out.println(json.toString());

}



{"infos":[{"contractid":2,"date_created":"","doc_num":"","full_name":"","realinfo_uid":"","title":""},{"contractid":3,"date_created":"","doc_num":"","full_name":"","realinfo_uid":"","title":""}]}