import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator.Feature;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.JavaType;
public class JsonUtil {
public final static ObjectMapper mapper = new ObjectMapper();
static{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mapper.setDateFormat(df);
mapper.setSerializationInclusion(Inclusion.NON_NULL);
mapper.configure(Feature.WRITE_NUMBERS_AS_STRINGS, false);
mapper.configure(Feature.WRITE_NUMBERS_AS_STRINGS, false);
}
private static JavaType getCollectionType(Class<?> collectionClass,Class<?>...classes){
return mapper.getTypeFactory().constructParametricType(collectionClass, classes);
}
/**
* json字符串转对象
* @param json
* @param c
* @return
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
public static <T> T jsonToObject(String json,Class<T> c) throws
JsonParseException, JsonMappingException, IOException{
if(json != null && !"".equals(json)){
return mapper.readValue(json,c);
}
return null;
}
/**
* json字符串转泛型集合
* @param json
* @param c
* @return
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
public static <T> List<T> jsonToObjectList(String json,Class<T> c) throws
JsonParseException, JsonMappingException, IOException{
List<T> result = null;
if(json != null && !"".equals(json)){
result = mapper.readValue(json,getCollectionType(ArrayList.class, c));
}
return result;
}
/**
* json字符串转Map
* @param json
* @return
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
public static Map jsonToMap(String json)
throws JsonParseException, JsonMappingException, IOException{
return jsonToObject(json, Map.class);
}
/**
* 集合转json字符串
* @param list
* @return
* @throws JsonGenerationException
* @throws JsonMappingException
* @throws IOException
*/
public static String objectListToString(List list)
throws JsonGenerationException, JsonMappingException, IOException{
if(list != null && list.size()>0){
return mapper.writeValueAsString(list);
}
return null;
}
/**
* 对象转json字符串
* @param o
* @return
* @throws JsonGenerationException
* @throws JsonMappingException
* @throws IOException
*/
public static String objectToString(Object o)
throws JsonGenerationException, JsonMappingException, IOException{
if(o!=null){
return mapper.writeValueAsString(o);
}
return "";
}
public static void main(String[] args) {
String s = "[{\"id\":\"0\",\"child\":[{\"id\":\"01\",\"name\":\"Tom\"},{\"id\":\"02\"}]},"
+ "{\"id\":\"1\",\"name\":\"Jerry\"}]";
try {
List<Map> list = JsonUtil.jsonToObjectList(s, Map.class);
System.out.println(list);//[{id=0, child=[{id=01, name=Tom}, {id=02}]}, {id=1, name=Jerry}]
for(Map<String,Object> map : list){
System.out.println(map.get("id"));//0 1
}
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
java操作Json工具类
最新推荐文章于 2022-06-28 01:47:27 发布