json转对象
JSONObject thumb = shopGoodsInfo.get("thumb");
UrlBo urlBo = JsonInfoUtil.jsonToObj(JsonInfoUtil.toJson(shopGoodsInfo.get("thumb")), UrlBo.class);
json转List
JSONObject thumb = shopGoodsInfo.get("thumb");
List<UrlBo> appPushBindList = JsonInfoUtil.jsonToObj(JsonInfoUtil.toJson(shopGoodsObject.getString("weightName")),new TypeReference<List<UrlBo>>(){});
json转Map
String thumb="";
Map<String, Object> map = JsonInfoUtil.jsonToObj(JsonInfoUtil.toJson(thumb), new TypeReference<Map<String, Object>>() {});
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang.StringUtils;
import java.util.Date;
import java.util.HashMap;
/**
*
* json转化对象
*
*/
public class JsonInfoUtil {
private final static String NULLSTR = "";
private final static String JSONP_PREFIX = "callback(";
private final static String JSONP_SUFFIX = ")";
private JsonInfoUtil() {};
private final static SerializeConfig config = new SerializeConfig();
{
config.put(Date.class, new JSONLibDataFormatSerializer());
config.put(java.sql.Date.class, new JSONLibDataFormatSerializer());
}
private final static SerializerFeature[] features = {
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNullStringAsEmpty
};
public final static SerializerFeature[] containDisableCircularReferenceDetect = {
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.DisableCircularReferenceDetect
};
private final static Feature[] parseFeatures = {
Feature.IgnoreNotMatch,
Feature.SortFeidFastMatch,
Feature.SupportArrayToBean
};
public static String toJson(Object object) {
return JSON.toJSONString(object, config, features);
}
public static String toJsonDisableCircularReferenceDetect(Object object){
return JSON.toJSONString(object, config, containDisableCircularReferenceDetect);
}
@Deprecated
public static String toJsonp(Object object) {
return toJson(object);
//return JSONP_PREFIX + JSON.toJSONString(object, config, features) + JSONP_SUFFIX;
}
public static <T> T jsonToObj(String jsonValue, Class<T> c) {
return StringUtils.isBlank(jsonValue) ? null : JSON.parseObject(jsonValue, c,parseFeatures);
}
public static <T> T jsonToObj(String jsonValue, TypeReference<T> typeReference){
return JSON.parseObject(jsonValue,typeReference,parseFeatures);
}
@Deprecated
public static <T> T jsonpToObj(String jsonpValue, Class<T> c) {
String rex = "[()]+";
if (StringUtils.isBlank(jsonpValue)) {
return null;
}
String[] json = jsonpValue.split(rex);
if (json.length < 1) {
throw new IllegalArgumentException("The params of 'jsonpValue' is not invalid,pls check.");
}
return JSON.parseObject(json[1],c);
}
public static void main(String[] args) {
HashMap<String, Date> map = new HashMap<>();
Date put = map.put("2", null);
String s = JsonInfoUtil.toJson(map);
System.out.println(s);
}
}