Java-json相关转换,JSONObject与实体类/map互转、List/List<map>和JSONArray互转、获取JSONObject中的key value、字符串String转换等

博客背景是Java开发。json相关的转换、取值等在日常开发中经常使用,但有时候还是会忘记or遇到些奇奇怪怪的问题。以此记录,提醒自己~不定期更新~

1、JSONObject相关

实体类和JSONObject互转

//实体类转为JSONObject
JSONObject jsonObject = JSON.toJSON(entity);
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(entity));
//JSONObject转为实体类
Entity entity = JSONObject.toJavaObject(jsonObject, Entity.class);

Map和JSONObject互转

//Map转为JSONObject
JSONObject jsonObject = JSON.toJSON(map);
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(map));
//JSONObject转为Map
Map<String, Object> map = JSONObject.toJavaObject(jsonObject, Map.class);

String转JSONObject

JSONObject  jsonObject = JSONObject.fromObject(stringText);

实体类转JSON字符串时不过滤NULL空值

String jsonStr = JSONObject.toJSONString(VO,SerializerFeature.WriteMapNullValue);

获取JSONObject中的key value

JSONObject baseInfo = jsonObject.getJSONObject("detail");
// 遍历基本信息
for (Map.Entry<String, Object> entry : baseInfo.entrySet()) {
    System.out.println(entry.getKey() + " : " + entry.getValue());
}

2、JSONArray相关

List和JSONArray互转

//List转为JSONArray
JSONArray jsonArray = JSON.toJSON(list);
JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
//JSONArray转为List
List<Entity> list = JSONObject.parseArray(jsonArray.toJSONString(), Entity.class);

List<Map<>>和JSONArray互转

//List<Map<>>转为JSONArray
JSONArray jsonArray = JSON.toJSON(maps);
JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(maps));

//JSONArray转List<hashmap>
JSONArray array = jsonObject.getJSONArray("array");
List<HashMap> objects = JSONObject.parseArray(JSONObject.toJSONString(array),HashMap.class);

//JSONArray转为List<Map<>>
List<Map<>> maps = JSONObject.parseArray(jsonArray.toJSONString(), Map.class);

//JSONArray转为List<Map<>>方法2
List<Map<String, String>> list=new ArrayList<Map<String, String>>();               
JSONArray jsonArr=JSONObject.fromObject(jsonStr).getJSONArray("data");
if(jsonArr!=null && jsonArr.size()>0){
	for(int i=0;i<jsonArr.size();i++){
		JSONObject jsonObj=(JSONObject) jsonArr.get(i);
		Map<String, String> map = new HashMap<String, String>();
		for (Iterator<?> iter = jsonObj.keys(); iter.hasNext(); ) {
			String key = (String) iter.next();
			String value = jsonObj.get(key).toString();
			map.put(key, value);
		}
		list.add(map);
    }

JASONArray转为JSONObject

JSONArray result_type = new JSONArray();
StringBuffer selectType = new StringBuffer();
selectType.append(" select id from table");
result_type = jdbcTemp.queryForJSONArray(selectType.toString());
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < result_type.size(); i++) {
    JSONObject jsonObject = (JSONObject) result_type.get(i);
}
return jsonObject;

解决问题:JASONArray转为JSONObject只返回了最后一个JSONObject

StringBuilder builder = new StringBuilder(); 

//业务代码

//转换
for (int i = 0; i < jsonArray.size(); i++) {
    jsonObject = (JSONObject) jsonArray.get(i);
    builder.append(jsonObject).append("n"); 
} 
//业务代码

return builder.toString();

String转JSONArray

JSONArray jsonArray = JSONArray.fromObject(stringText);

3、Object相关

Object转JSONObject

JSONObject json = (JSONObject) JSONObject.toJSON(list.get(0));

Object转List

	//Object转List<String>
	public static List<String> objToList1(Object obj) throw CoreException{
		List<String> result = new ArrayList<String>();
    	if (obj instanceof ArrayList<?>) {
	        for (Object o : (List<?>) obj) {
	            result.add(String.class.cast(o));
	        }
        }
        return result;
	}
    //通用工具类
  	public static <T> List<T> objToList(Object obj, Class<T> cla) throw CoreException{
		List<T> list = new ArrayList<T>();
    	if (obj instanceof ArrayList<?>) {
	        for (Object o : (List<?>) obj) {
	            list.add(cla.cast(o));
	        }
	        return list;
        }
        return null;
	}
    //调用
 	List<String> list = objToList(obj,String.class);
	//注意:int.class = Integer.TYPE !=Integer.class