// 将对象转换成Json格式的字符串
public static String convertObject2Json(Object object) {
Gson gson=new Gson();
return gson.toJson(object);
}
// 将JSON转换成java对象
// 第一种方法
public static Object convertJson2Object(InputStream inputStream,Class<?>javaBean ,int convertFlag) {
gson=new Gson();
Object object=null;
// String json=inputStream2String(inputStream);
BufferedReader reader=intputStream2BufferedReader(inputStream);
Type type=getType(javaBean,convertFlag);
object=gson.fromJson(reader,type);
return object;
}
// 第二种方法
public static Object convertJson2Object(String str,Class<?>javaBean ,int convertFlag) {
gson=new Gson();
Object object=null;
// String json=inputStream2String(inputStream);
// BufferedReader reader=intputStream2BufferedReader(inputStream);
Type type=getType(javaBean,convertFlag);
object=gson.fromJson(str,type);
return object;
}
private static BufferedReader intputStream2BufferedReader(InputStream inputStream) {
return new BufferedReader(new InputStreamReader(inputStream));
}