【Java】+获取JSON串key名称及key对应值的类型


修改json指定路径的值:https://blog.csdn.net/liupeifeng3514/article/details/79180154

代码:

    public static void main(String[] args) {
        String str = "{\"bussDatas\":[{\"fieldDesc\":\"string\",\"isSelected\":0,\"optionType\":0,\"optionValue\":\"string\",\"orderNum\":0,\"placeHolder\":\"string\"}],\"moduleName\":\"string\",\"packageId\":\"string\",\"techDatas\":{\"fieldDesc\":\"string\",\"isSelected\":0,\"optionType\":0,\"optionValue\":\"string\",\"orderNum\":0,\"placeHolder\":\"string\"}}\n";
        JSONObject jsonObject = JSONObject.parseObject(str);

        // 格式化输出JSON
        String pretty = JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
        System.out.println(String.format("原始JSON:\r\n%s", pretty));

        // 获取JSON第一层所有的key
        Set<String> keys = jsonObject.keySet();
        // 获取第一层每个key对应的值 的类型
        for (String key : keys) {
            System.out.println(String.format("%s(key):%s(值类型)", key, jsonObject.get(key).getClass().getSimpleName()));
        }
    }

输出:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM