其他分享
首页 > 其他分享> > 简单实用工具之Json报文自动转换类

简单实用工具之Json报文自动转换类

作者:互联网

最近几天在对接其他系统,我们用的是java,其他系统可能不是java。所以在报文格式上有点区别:
收到的报文如下:

{
	"id": 27640,
     "project_name": "WEB",
     "subsystem_name": "WEB",
     "pd_fullname2": "WEB",
     "app_name": "hotdb管理节点",
     "app_cname": "hotdb管理节点",
     "app_type": "Mysql",
     "app_id": 2584,
     "app_owner": "灵",
     "maintain_user_name": [
         "灵"
     ],
     "ops_user_name": [
         "杰",
         "旺"
     ],
     "resource_type_name": "物理机",
     "resource_env_name": "生产环境",
     "status_name": "空闲中",
     "os_type_name": "CentOS",
     "ip_manage": [
         [
             4,
             "10.99.2.22"
         ]
     ],
     "update_time": "2021-02-23 15:58",
     "change_user_name": "01424191",
     "account_manage_id": 3738,
     "app": 2584,
     "is_ping_name": "Up",
     "owner": "002558512",
     "maintain_user": "002558512",
     "ops_user": "015083912,016303316",
     "change_user": "014241911",
     "create_time": "2020-03-17T14:43:00.129284",
     "host_name": "srvmid1110",
     "ip": "10.6.111.122",
     "ip_extend": 0,
     "resource_type": 1,
     "resource_env": 1,
     "status": 3,
     "os_type": 1,
     "os_version": "7.6",
     "cpu": 32,
     "memory": 251,
     "disk": 9217,
     "maintainer": "",
     "comments": "D",
     "is_ping": 1,
     "in_reserve": false,
     "in_belong": ""
}

而我想要生成实体类是这样的:

private int id;
@JsonProperty("project_name")
private String projectName;
@JsonProperty("subsystem_name")
private String subsystemName;
@JsonProperty("pd_fullname2")
private String pdFullname2;
@JsonProperty("app_name")
private String appName;
@JsonProperty("app_cname")
private String appCname;
@JsonProperty("app_type")
private String appType;
@JsonProperty("app_id")
private int appId;
@JsonProperty("app_owner")
private String appOwner;
@JsonProperty("maintain_user_name")
private List<Object> maintainUserName;
@JsonProperty("ops_user_name")
private List<Object> opsUserName;
@JsonProperty("resource_type_name")
private String resourceTypeName;
@JsonProperty("resource_env_name")
private String resourceEnvName;
@JsonProperty("status_name")
private String statusName;
@JsonProperty("os_type_name")
private String osTypeName;
@JsonProperty("ip_manage")
private List<Object> ipManage;
@JsonProperty("update_time")
private String updateTime;
@JsonProperty("change_user_name")
private String changeUserName;
@JsonProperty("account_manage_id")
private int accountManageId;
private int app;
@JsonProperty("is_ping_name")
private String isPingName;
private String owner;
@JsonProperty("maintain_user")
private String maintainUser;
@JsonProperty("ops_user")
private String opsUser;
@JsonProperty("change_user")
private String changeUser;
@JsonProperty("create_time")
private String createTime;
@JsonProperty("host_name")
private String hostName;
private String ip;
@JsonProperty("ip_extend")
private int ipExtend;
@JsonProperty("resource_type")
private int resourceType;
@JsonProperty("resource_env")
private int resourceEnv;
private int status;
@JsonProperty("os_type")
private int osType;
@JsonProperty("os_version")
private String osVersion;
private int cpu;
private int memory;
private int disk;
private String maintainer;
private String comments;
@JsonProperty("is_ping")
private int isPing;
@JsonProperty("in_reserve")
@JsonProperty("in_belong")
private String inBelong;

于是就顺手写了个生成类:

package com.yt.study.utils;

import java.util.List;
import java.util.Map;

public class GenerateCode {

    public String generate(String json) {
        StringBuffer sb = new StringBuffer();
        String prefix = "private ";
        Map<String, Object> map = JacksonUtil.from(json, Map.class);
        map.forEach((k, v) -> {
            String result = "";
            String[] words = k.split("_");
            for (int i = 0; i < words.length; i++) {
                if (i == 0) {
                    result += words[i];
                } else {
                    String c = toUpperFirstCase(words[i]);
                    result += c;
                }
            }
            if (words.length > 1) {
                sb.append("@JsonProperty(\"" + k + "\")\n");
            }
            if (v instanceof Integer) {
                sb.append(prefix + "int " + result + ";\n");
            }
            if (v instanceof List) {
                sb.append(prefix + "List<Object> " + result + ";\n");
            }
            if (v instanceof String) {
                sb.append(prefix + "String " + result + ";\n");
            }
            if (v == null) {
                sb.append(prefix + "String " + result + ";\n");
            }
        });
        return sb.toString();
    }

    public String toUpperFirstCase(String c) {
        char[] chars = c.toCharArray();
        String s = "";
        for (int i = 0; i < chars.length; i++) {
            if (i == 0) {
                String s1 = chars[0] + "";
                s1 = s1.toUpperCase();
                s = s1;
            } else {
                s = s + chars[i];
            }
        }
        return s;
    }
}

package com.yt.study;

import com.yt.study.utils.GenerateCode;

public class JsonGenerateCode {
    public static void main(String[] args) {
        GenerateCode generateCode = new GenerateCode();
        String json = " {\n" +
                "\t\"id\": 27640,\n" +
                "     \"project_name\": \"WEB\",\n" +
                "     \"subsystem_name\": \"WEB\",\n" +
                "     \"pd_fullname2\": \"WEB\",\n" +
                "     \"app_name\": \"hotdb管理节点\",\n" +
                "     \"app_cname\": \"hotdb管理节点\",\n" +
                "     \"app_type\": \"Mysql\",\n" +
                "     \"app_id\": 2584,\n" +
                "     \"app_owner\": \"灵\",\n" +
                "     \"maintain_user_name\": [\n" +
                "         \"灵\"\n" +
                "     ],\n" +
                "     \"ops_user_name\": [\n" +
                "         \"杰\",\n" +
                "         \"旺\"\n" +
                "     ],\n" +
                "     \"resource_type_name\": \"物理机\",\n" +
                "     \"resource_env_name\": \"生产环境\",\n" +
                "     \"status_name\": \"空闲中\",\n" +
                "     \"os_type_name\": \"CentOS\",\n" +
                "     \"ip_manage\": [\n" +
                "         [\n" +
                "             4,\n" +
                "             \"10.99.2.22\"\n" +
                "         ]\n" +
                "     ],\n" +
                "     \"update_time\": \"2021-02-23 15:58\",\n" +
                "     \"change_user_name\": \"01424191\",\n" +
                "     \"account_manage_id\": 3738,\n" +
                "     \"app\": 2584,\n" +
                "     \"is_ping_name\": \"Up\",\n" +
                "     \"owner\": \"002558512\",\n" +
                "     \"maintain_user\": \"002558512\",\n" +
                "     \"ops_user\": \"015083912,016303316\",\n" +
                "     \"change_user\": \"014241911\",\n" +
                "     \"create_time\": \"2020-03-17T14:43:00.129284\",\n" +
                "     \"host_name\": \"srvmid1110\",\n" +
                "     \"ip\": \"10.6.111.122\",\n" +
                "     \"ip_extend\": 0,\n" +
                "     \"resource_type\": 1,\n" +
                "     \"resource_env\": 1,\n" +
                "     \"status\": 3,\n" +
                "     \"os_type\": 1,\n" +
                "     \"os_version\": \"7.6\",\n" +
                "     \"cpu\": 32,\n" +
                "     \"memory\": 251,\n" +
                "     \"disk\": 9217,\n" +
                "     \"maintainer\": \"\",\n" +
                "     \"comments\": \"D\",\n" +
                "     \"is_ping\": 1,\n" +
                "     \"in_reserve\": false,\n" +
                "     \"in_belong\": \"\"\n" +
                "}";
        String generate = generateCode.generate(json);
        System.err.println(generate);
    }
}

方便自己下次使用。
运行结果如图:
在这里插入图片描述

标签:JsonProperty,name,实用工具,private,Json,user,app,报文,String
来源: https://blog.csdn.net/u012663412/article/details/120302808