Conversion between Json and Java objects through Gson Learning
Conversion between Json and Java objects through Gson Learning
Please respect the fruits of others' work, and repost them with the source:Conversion between Json and Java objects through Gson Learning
I have introduced Xstream conversion between XML, Object, and Json in XML, Object, and Json conversion, although XStream has built-in support for Json, the effect is not very good. After all, XStream does not focus on Json. Next we will introduce another tool.
Gson(Also known as Google Gson) is an open-source Java Library released by Google. It mainly uses serialized Java objects as JSON strings or deserialized JSON strings as Java objects.
1. How to Use Gson?
First from http://code.google.com/p/google-gson/downloads/listDownload the jar package of Gson and import it to the project. Then you can use Gson in your project.
2.
Main Method
Gson is mainly usedToJsonAndFromJsonTwo conversion methods: before using this type of object conversion, you must first create an object class and its members to successfully convert the JSON string to the corresponding object.
3. Gson application example
/*** Use Gson to implement object, Json conversion * @ author jph * Date: 2014.09.29 */public class GsonUtil {public static Gson gson; /** convert Json to JavaBean **/public static final int JSON_JAVABEAN = 0x10001;/** convert Json to List
**/Public static final int JSON_LIST = 0x10002;/** convert Json to Map
**/Public static final int JSON_MAP = 0x10004;/*** convert an object to a Json String * @ param object to a Json object * @ return String: json String */public static String convertObject2Json (Object object) {gson = new Gson (); return gson. toJson (object );} /*** convert Json to a Java object * @ param inputStream to the inputStream of a Java object * @ param javaBean List to obtain the javaBean in Map * @ param convertFlag conversion 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 * @ 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) {// convert Json to List generic type = new TypeToken
> () {}. GetType ();} break; case JSON_MAP: if (javaBean. equals (News. class) {// convert Json to Map generic type = new TypeToken
> (){}. GetType () ;}break; case JSON_JAVABEAN: // convert JavaBean type = javaBean; break;} return type ;} /*** encapsulate InputStream into BufferedReader * @ param inputStream * @ return */private static BufferedReader intputStream2BufferedReader (InputStream inputStream) {return new BufferedReader (new InputStreamReader (inputStream ));}}
Code Analysis:
When Json is converted to an object, the code above obtains the input stream from the server, encapsulates the input stream into a BufferedReader object, and converts the Json to a Java object using the fromJson () method.
The first parameter of the fromJson () method of Gson supports parameters of the String, JsonElement, and Reader types and can be selected as needed. The second parameter of the fromJson () method supports Type and Class. Type parameter. Class can be used when Json is converted to JavaBean. Parameter, that is, the JavaBean. calss of the corresponding JavaBean is used as the second parameter. When you need to convert Json to List generic Type and Map generic Type, you need to use TypeToken to convert the second parameter to Type (TypeToken is the data Type converter provided by gson, supports conversion of various data set types .).