作者:416703721 | 来源:互联网 | 2023-10-12 11:23
1.map转json
Map map = new HashMap();
map.put("a", "a");
map.put("b", "123");
JSONObject json = new JSONObject(map);
2.map转string
Map map = new HashMap<>();
map.put("a", "b");
String s = JSONObject.toJSONString(map);
3.json转string
JSONObject json = new JSONObject();
json.put("c", "v");
json.put("z", "123n);
json.toJSONString();
4.json转map
JSONObject json = new JSONObject();
json.put("ccc", "321");
json.put("bbb", "123");
Map map = (Map)json;
5.string转json
String str = "{\"username\":\"dsad\",\"qwewqe\":\"123\"}";
JSONObject json = JSONObject.parseObject(str);
6.json转map
private static Object json_to_map(String json)
{
Object array = JSONObject.parse(json);
if(array instanceof JSONArray){
List
判断是否为json串
/*
*字符串{},[]都是正确的json串
*/
public static boolean isJson(String str){
if (StringUtils.isBlank(str)){
return false;
}
try{
Object parse = JSON.parse(str);
if (parse instanceof JSONObject || parse instanceof JSONArray){
return true;
}
}catch (Exception e){
return false;
}
return false;
}
判断Object类型
Object ret = json_to_map(jsonmap);
log.info(ret == null ? "null" : ret instanceof HashMap ? "HashMap" : ret instanceof ArrayList ? "ArrayList" : ret.getClass().getTypeName());