There are now some open source projects that can convert Java objects into JSON. But most projects require you to include Java annotations in the class files, which cannot be done when you cannot change the source code. And they also do not support Java generics. But Gson takes these two points as his very important design goals.
Features:
Gson's application mainly includes two conversion methods: toJson and fromJson. Before using this object conversion, you need to create the object's class and its members to successfully convert the JSON string into the corresponding object.
public class GsonUtil { public static Gson gson; /**Json to JavaBean**/ public static final int JSON_JAVABEAN=0x10001; /**Json to List<T>**/ public static final int JSON_LIST=0x10002; /**Json to Map<T>**/ public static final int JSON_MAP=0x10004; /** * Convert an object to a string in Json format* @param object To be converted to an object in Json* @return String: string in Json format*/ public static String convertObject2Json(Object object) { gson=new Gson(); return gson.toJson(object); } /** * Convert Json to Java object* @param inputStream inputStream to convert to Java object * @param javaBean List Get the javaBean contained in the map * @param convertFlag Convert type identifier* @return Object:Java object*/ 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; } /** * Get the object type to be converted to* @param javaBean * @param convertFlag * @return */ private static Type getType(Class<?> javaBean,int convertFlag) { Type type=null; switch (convertFlag) { case JSON_LIST: if (javaBean.equals(News.class)) {//Json to List generic type=new TypeToken<List<News>>(){}.getType(); } break; case JSON_MAP: if (javaBean.equals(News.class)) {//Json to Map generic type=new TypeToken<Map<String,News>>(){}.getType(); } break; case JSON_JAVABEAN://Json to JavaBean type=javaBean; break; } return type; } /** * Encapsulate InputStream into BufferedReader * @param inputStream * @return */ private static BufferedReader inputStream2BufferedReader(InputStream inputStream) { return new BufferedReader(new InputStreamReader(inputStream)); }}Code analysis:
When converting Json to an object, the above code is to obtain the input stream from the server side, then encapsulate the input stream into a BufferedReader object, and then convert Json to a Java object through the fromJson() method.
The first parameter of Gson's fromJson() method supports parameters of type String, JsonElement and Reader, and can be selected according to your needs. The second parameter of the fromJson() method supports parameters of Type and Class<?>. When converting Json to JavaBean, you can use the Class<?> parameter, that is, use the JavaBean.cals corresponding to JavaBean as the second parameter. When you need to convert Json into List generics, and when Map generics, you need to use TypeToken to convert the second parameter to Type type (TypeToken is a data type converter provided by gson, which can support various data collection type conversion).