首页 > 代码库 > json格式转换
json格式转换
json数据转换
import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.DeserializationConfig.Feature; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class JsonParse { /** * 转换成json数据 * * @param obj * @return */ public static String getJson(Object obj) { if (obj == null) return null; ObjectMapper om = new ObjectMapper(); StringWriter sw = new StringWriter(); try { JsonGenerator jg = new JsonFactory().createJsonGenerator(sw); om.writeValue(jg, obj); jg.close(); } catch (IOException e) { e.printStackTrace(); } return sw.toString(); } /** * 将json数据转换成List<Object> * * @param json * @param entityClass * @return */ public static <T> List<T> getList(String json, Class<T> entityClass) { if (StringUtils.isBlank(json)) return null; List<T> list = new ArrayList<T>(); ObjectMapper om = new ObjectMapper(); om.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { JSONArray array = new JSONArray(json); for (int i = 0; i < array.length(); i++) { T t = om.readValue(array.getString(i), entityClass); list.add(t); } return list; } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return null; } /** * 将json数据转换成List<LinkedHashMap<String, Object>> * * @param json * @return */ @SuppressWarnings("unchecked") public static List<LinkedHashMap<String, Object>> getListMap(String json) { if (StringUtils.isBlank(json)) return null; ObjectMapper om = new ObjectMapper(); om.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { List<LinkedHashMap<String, Object>> list = om.readValue(json, List.class); return list; } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 将json数据转换成List<Map<String, T>> * * @param json * @return */ public static <T> List<Map<String, T>> getListMap(String json, Class<T> entityClass) { if (StringUtils.isBlank(json)) return null; ObjectMapper om = new ObjectMapper(); om.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { List<Map<String, T>> list = om.readValue(json, new TypeReference<List<Map<String, T>>>() { }); return list; } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 将json数据转换成Map<String, Object> * * @param json * @return */ @SuppressWarnings("unchecked") public static Map<String, Object> getMap(String json) { if (StringUtils.isBlank(json)) return null; ObjectMapper om = new ObjectMapper(); om.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { Map<String, Object> map = om.readValue(json, Map.class); return map; } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 将json数据转换成Map<String, T> * * @param json * @return */ public static <T> Map<String, T> getMap(String json, Class<T> entityClass) { if (StringUtils.isBlank(json)) return null; ObjectMapper om = new ObjectMapper(); om.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { Map<String, T> list = om.readValue(json, new TypeReference<Map<String, T>>() { }); return list; } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 将json数据转换成指定的类型 * * @param json * @param valueType * @return */ public static <T> T getObject(String json, Class<T> valueType) { if (StringUtils.isBlank(json)) return null; ObjectMapper om = new ObjectMapper(); om.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { return om.readValue(json, valueType); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }
在BaseControl中可以写一个这样的方法
/** * 输出HTML脚本 */ protected void writeJson(HttpServletResponse response, Object obj) { this.writeJson(response, obj, "utf-8"); } /** * 输出HTML脚本带格式 */ protected void writeJson(HttpServletResponse response, Object obj, String charset) { String json = JsonParse.getJson(obj); response.setContentType("text/html;charset=" + charset); response.setDateHeader("Expires", -10); PrintWriter out = null; try { out = response.getWriter(); out.println(json); out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) out.close(); } }
在使用时,继承BaseControl,调用writeJson方法,返回给前台的是json格式的字符串。
this.writeJson(response, helloService.getSomething(id));
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。