JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,她是 JavaScript原生态的数据格式。
她独立于任何编程语言、可读性高、易于生成和解析、体积轻量,这些优雅的特性,让JSON成为非常理想的数据交换的语言。
在使用Java操作JSON时,我们用到了Jackson框架。
Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。
Jackson相关
下载依赖库jar包
Jackson的jar all下载地址:http://jackson.codehaus.org/1.7.6/jackson-all-1.7.6.jar
然后在工程中导入这个jar包即可开始工作
官方示例:http://wiki.fasterxml.com/JacksonInFiveMinutes
下面为基于Jackson的JsonUtils操作工具类:
-
-
-
-
-
-
public class JSONUtils {
-
-
private static Logger logger = Logger.getLogger(JSONUtils.class);
-
-
-
-
-
-
-
public static Map<String, String> JsonToMap(String jsonStr) {
-
try {
-
ObjectMapper objectMapper = new ObjectMapper();
-
Map<String, String> params = objectMapper.readValue(jsonStr,Map.class);
-
return params;
-
} catch (Exception e) {
-
logger.error("JsonToMap Error " + jsonStr, e);
-
return null;
-
}
-
}
-
-
-
-
-
-
-
public static Map<String, Object> readJson2Map(String jsonStr) {
-
try {
-
ObjectMapper objectMapper = new ObjectMapper();
-
Map<String, Object> params = objectMapper.readValue(jsonStr,Map.class);
-
return params;
-
} catch (Exception e) {
-
logger.error("JsonToMap Error " + jsonStr, e);
-
return null;
-
}
-
}
-
-
-
-
-
-
-
-
public static <T> T readJson2Bean(String jsonStr,Class<T> clazz) {
-
try {
-
ObjectMapper objectMapper = new ObjectMapper();
-
T t = objectMapper.readValue(jsonStr,clazz);
-
return t;
-
} catch (Exception e) {
-
logger.error("readJson2Bean Error " + jsonStr, e);
-
return null;
-
}
-
}
-
-
-
-
-
-
-
public static List<HashMap<String, Object>> JsonToMapList(String jsonStr) {
-
try {
-
ObjectMapper objectMapper = new ObjectMapper();
-
List<HashMap<String, Object>> params = objectMapper.readValue(jsonStr,List.class);
-
for (int i = 0; i < params.size(); i++) {
-
Map<String, Object> map = params.get(i);
-
Set<String> set = map.keySet();
-
for (Iterator<String> it = set.iterator();it.hasNext();) {
-
String key = it.next();
-
}
-
}
-
return params;
-
} catch (Exception e) {
-
logger.error("JsonToMap Error " + jsonStr, e);
-
return null;
-
}
-
}
-
-
-
-
-
-
-
public static String WriteToJson(Object... paramObjs){
-
try {
-
if(paramObjs==null||paramObjs.length==0){
-
logger.error("参数实例为空");
-
}
-
Map<String,Object> paramMap = new HashMap<String, Object>();
-
for(int i=0;i<paramObjs.length;i+=2){
-
paramMap.put((String) paramObjs[i], paramObjs[i+1]);
-
}
-
ObjectMapper objectMapper = new ObjectMapper();
-
return objectMapper.writeValueAsString(paramMap);
-
} catch (Exception e) {
-
return "";
-
}
-
}
-
-
-
-
-
-
-
public static Map<String, String> JsonToMapAndDecode(String result) {
-
Map<String, String> params = JsonToMap(result);
-
for (String key : params.keySet()) {
-
params.put(key, HttpUtils.urlDecode(params.get(key), "UTF-8"));
-
}
-
return params;
-
}
-
-
public static String toJson(Object target){
-
String result = null;
-
try {
-
ObjectMapper objectMapper = new ObjectMapper();
-
result = objectMapper.writeValueAsString(target);
-
} catch (Exception e) {
-
logger.error("convert to Json Str Fail !");
-
e.printStackTrace();
-
}
-
return result;
-
}
-
}
最后,Jackson对XML的支持
-
public void writeObject2Xml() {
-
-
System.out.println("XmlMapper");
-
XmlMapper xml = new XmlMapper();
-
-
try {
-
-
-
StringWriter sw = new StringWriter();
-
xml.writeValue(sw, bean);
-
System.out.println(sw.toString());
-
-
List<AccountBean> list = new ArrayList<AccountBean>();
-
list.add(bean);
-
list.add(bean);
-
System.out.println(xml.writeValueAsString(list));
-
-
-
Map<String, AccountBean> map = new HashMap<String, AccountBean>();
-
map.put("A", bean);
-
map.put("B", bean);
-
System.out.println(xml.writeValueAsString(map));
-
} catch (JsonGenerationException e) {
-
e.printStackTrace();
-
} catch (JsonMappingException e) {
-
e.printStackTrace();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
Java使用jackson操作Json格式数据
原文:http://blog.csdn.net/oydaybreak/article/details/45170557