HashMap转换成json实例

来源:互联网 发布:菊花插件数据下载 编辑:程序博客网 时间:2024/06/07 03:19

@1创建的Account类:

public class Account implements Serializable{/** * userId:用户id * userName:用户名称 * status:用户状态 * orgUUID:用户所属HR系统部门主键 * mobile:手机号码 * email:电子邮件 */private static final long serialVersionUID = 1L;private String userId;private String userName;private String status;private String orgUUID;private String mobile;private String email;}


@2HashMap转换成json方法

public JSONObject resposeJson(){JSONObject allJson = new JSONObject();HashMap<String, Object> hashMap = new HashMap<String, Object>();hashMap.put("SERVICE_ID", "120030002");hashMap.put("SCENE_ID", "120030002");hashMap.put("SOURCE_TYPE", "120030002");hashMap.put("PRVD_SYS_ID", "120030002");hashMap.put("RUN_DATE", "120030002");hashMap.put("TRAN_DATE", "120030002");hashMap.put("TRAN_TIMESTAMP", "120030002");hashMap.put("SEQ_NO", "120030002");hashMap.put("REFERENCE", "120030002");hashMap.put("RET_STATUS", "120030002");HashMap<String, String> hashMap2 = new HashMap<String, String>();hashMap2.put("RET_CODE", "000000");hashMap2.put("RET_MSG", "success");hashMap.put("RET", hashMap2);HashMap<String, Object> hashMap3 = new HashMap<String, Object>();Account account = new Account();account.setUserId("101");account.setUserName("cheney");account.setEmail("89@qq.com");account.setMobile("133113234");account.setOrgUUID("水果部门");account.setStatus("e");Account account2 = new Account();account2.setUserId("102");account2.setUserName("cheney1993");account2.setEmail("160@qq.com");account2.setMobile("13631223892");account2.setOrgUUID("菜鸡部门");account2.setStatus("e");List<Account> listAccounts = new ArrayList<Account>();listAccounts.add(account);listAccounts.add(account2);hashMap3.put("account", listAccounts);hashMap3.put("reponseJson", "test");allJson.put("SYS_HEAD", hashMap);allJson.put("BODY", hashMap3);return allJson;}


@3转换后的json格式化结果显示如下:

{    "SYS_HEAD": {        "SOURCE_TYPE": "120030002",        "RET": {            "RET_MSG": "success",            "RET_CODE": "000000"        },        "REFERENCE": "120030002",        "TRAN_TIMESTAMP": "120030002",        "RUN_DATE": "120030002",        "SCENE_ID": "120030002",        "RET_STATUS": "120030002",        "SERVICE_ID": "120030002",        "PRVD_SYS_ID": "120030002",        "TRAN_DATE": "120030002",        "SEQ_NO": "120030002"    },    "BODY": {        "account": [            {                "email": "89@qq.com",                "mobile": "133113234",                "orgUUID": "水果部门",                "status": "e",                "userId": "101",                "userName": "cheney"            },            {                "email": "160@qq.com",                "mobile": "13631223892",                "orgUUID": "菜鸡部门",                "status": "e",                "userId": "102",                "userName": "cheney1993"            }        ],        "reponseJson": "test"    }}