介绍
在平时开发过程中,经常需要对json进行操作,比如我最近都忙于第3方电商平台(天猫,京东、淘宝、唯品会、苏宁、小红书、寺库、网易考拉)进行对接(主要包括商品同步,库存,订单对接),除了天猫,京东、淘宝、唯品会、苏宁电商渠道对接各自都会提供一套封装好的sdk,然而小红书、寺库、网易考拉的对接是没有提供sdk进行开发,现在的操作方式 是通过调用http请求,通过put、post、get方式发送请求,请求成功后,会返回一个json格式的字符串。
如寺库调用secoo.brand.list接口获取品牌信息时
请求参数:
{format=JSON,method=secoo.brand.list,productCategoryId=31,sign=E3D796A63BFF2DFD34F408B6B10532E7,signMethod=md5,timestamp=2017-09-27 16:51:55,v=1.0,vendorCode=3059}
返回的json
{"codeMsg":"SUCCESS","data":[{"cnName":"路易威登","enName":"Louis Vuitton","id":2},{"cnName":"卡地亚","enName":"CARTIER","id":9},{"cnName":"赛琳","enName":"CELINE","id":10},{"cnName":"登喜路","enName":"DUNHILL","id":13},{"cnName":"菲拉格慕","enName":"Salvatore Ferragamo","id":16},{"cnName":"兰姿","enName":"LANCEL","id":22},{"cnName":"葆蝶家","enName":"Bottega Veneta","id":35},{"cnName":"圣罗兰","enName":"Yves saint Laurent","id":36},{"cnName":"万宝龙","enName":"MontBlanc","id":38},{"cnName":"巴黎世家","enName":"Balenciaga","id":40},{"cnName":"托德斯","enName":"Tod’s","id":41},{"cnName":"橘滋","enName":"Juicy Couture","id":42},{"cnName":"波士","enName":"BOSS","id":46},{"cnName":"都彭","enName":"S.T. DUPONT","id":75},{"cnName":"乔治阿玛尼","enName":"Giorgio Armani","id":85},{"cnName":"蒂芙尼","enName":"Tiffany & Co.","id":88},{"cnName":"盖尔斯","enName":"Guess","id":98},{"cnName":"楚莎迪","enName":"Trussardi","id":101},{"cnName":"吉尔·桑达","enName":"Jil Sander","id":112},........}],"code":200}
每个接口返回的都是类似的json字符串,为了方便对json字符串操作,一般都会先把json格式的字符串转化成对应的javabean对象或者一个javabean的list的对象。
有的时候可能会讲bean、list对象转换成json字符串以便在不同系统之间进行传输。鉴于此非常有必要对json字符串的操作进行一个封装,因此产生了jsonkit工具类。
JsonKit详解
主要包括4个方法
1、toBean 将json转成bean
2、toList 将json转换成List
3、toJson(Object bean) 将bean转成json
4、toJson(List list)
package com.trendy.fw.common.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonKit {
private static JsonFactory jsonFactory = new JsonFactory();
private static ObjectMapper mapper = null;
static {
jsonFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES,true);
jsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,true);
mapper = new ObjectMapper(jsonFactory);
}
/** * 获取jackson json lib的ObjectMapper对象 * * @return ObjectMapper对象 */
public static ObjectMapper getObjectMapper() {
return mapper;
}
/** * 获取jackson json lib的JsonFactory对象 * * @return JsonFactory对象 */
public static JsonFactory getJsonFactory() {
return jsonFactory;
}
/** * 将json转成bean * * @param <T> * 多态类型 * @param json * json字符串 * @param clazz * java bean类型(Class) * @return java bean对象 */
public static <T> T toBean(String json,Class<T> clazz) {
T bean = null;
try {
bean = mapper.readValue(json,clazz);
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException(e);
}
return bean;
}
/** * 将json转换成List * * @param json * json字符串 * @param clazz * java bean类型(Class) * @return */
public static <T> List<T> toList(String json,Class<T> clazz) {
List<T> list = new ArrayList<T>();
List<HashMap<String,Object>> jsonList = toBean(json,ArrayList.class);
for (HashMap<String,Object> map : jsonList) {
try {
list.add(mapper.convertValue(map,clazz));
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException(e);
}
}
return list;
}
/** * 将bean转成json * * @param bean * java bean * @return json 字符串 */
public static String toJson(Object bean) {
String rtv = null;
try {
rtv = mapper.writeValueAsString(bean);
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException(e);
}
return rtv;
}
/** * 将List转换成json * * @param list * @return */
public static String toJson(List<Object> list) {
String rtv = null;
StringBuffer sb = new StringBuffer();
try {
for (Object bean : list) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(mapper.writeValueAsString(bean));
}
rtv = "[" + sb.toString() + "]";
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException(e);
}
return rtv;
}
}
备注:以上代码需要依赖jackson json jar包