Java 常用工具类(34) : json转对象字段


import com.alibaba.fastjson.JSONObject;

import java.util.Set;

public class Json转对象 {

    private static JSONObject j;

    public static void main(String[] args) {
        j = JSONObject.parseObject(s);
        Set keys = j.keySet();

        // 遍历键并获取其类型
        for (String key : keys) {
            Class keyType = j.get(key).getClass();
            System.out.println("    \n" +
                    "    /**\n" +
                    "     * -\n" +
                    "     */\n" +
                    "    public " + getJavaObject(keyType.toString()) + " " + key + ";");
        }
    }

    public static String getJavaObject(String kt) {
        switch (kt) {
            case "java.lang.String":
                return "String";
            case "java.lang.Integer":
            case "java.lang.Long":
                return "Long";
            case "java.lang.Double":
            case "java.lang.Float":
            case "class java.math.BigDecimal":
                return "Double";
            default:
                break;
        }
        return "String";
    }


    public static String s = "{\n" +
            "            \"age\": \"6\",\n" +
            "            \"name\": \"张三\",\n" +
            "            \"speed\": 553.8017520000001,\n" +
            "            \"lng\": 103.980244,\n" +
            "            \"lat\": 30.694589\n" +
            "        }";
}

maven依赖

        
            com.alibaba
            fastjson
            1.2.83
        

你可能感兴趣的:(#,Java,常用工具类,java,python,开发语言,json生成对象)