【Java】+获取JSON串key名称及key对应值的类型
代码:
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())); } }
输出:
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25