Java JSON format Conversion Library Gson's preliminary use notes _java

Source: Internet
Author: User
Tags goto object object tojson

There are already open source projects that convert Java objects to JSON. But most projects require you to add Java annotations to the class file, which is not possible when you can't change the source code. And they do not support Java generics either. But Gson the two points as a very important design goal.

Characteristics:

    • Using the Tojson () and Fromjson () methods, you can easily complete the conversion of Java objects to JSON.
    • The ability to convert pre-existing objects that cannot be modified with JSON.
    • Supports the use of Java generics.
    • Allows the personalized expression of an object (representation).
    • Supports various objects that are complex (with deep inheritance hierarchies and extensive use of generic types).

The application of Gson is mainly for the Tojson and Fromjson two conversion methods, and the class of the object that needs to be created before using this object conversion can successfully convert the JSON string to the corresponding object.

public class Gsonutil {public static Gson Gson;
  /**json turn javabean**/public static final int json_javabean=0x10001;
  /**json turn list<t>**/public static final int json_list=0x10002;

  /**json turn map<t>**/public static final int json_map=0x10004; /** * Converts an object to a JSON-formatted string * @param object to be converted to JSON * @return String:json format string/public static string Conve      
    Rtobject2json (object) {gson=new Gson ();
  return Gson.tojson (object);
   /** * Converts JSON to Java objects * @param inputstream inputstream * @param javaBean list to be converted to Java objects gets the JavaBean contained in the map * @param convertflag Conversion type identification * @return Object:java object/public static objects Convertjson2object (InputStream inputs  
    Tream,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; /** * Gets the object type to convert * @param javaBean * @param convertflag * @return/private static Type GetType (Cla
    Ss<?> javabean,int convertflag) {Type type=null; Switch (convertflag) {case json_list:if (javabean.equals (News.class)) {//json goto LIST generic Type=new Typetoke
      N<list<news>> () {}.gettype ();
    } break; Case Json_map:if (Javabean.equals (News.class)) {//json goto MAP generic Type=new TYPETOKEN&LT;MAP&LT;STRING,NEWS&GT;&G
      t; () {}.gettype ();    
    } break;
      Case Json_javabean://json turn JAVABEAN Type=javabean;    
    Break 
  } return type; /** * InputStream packaged into BufferedReader * @param inputstream * @return/private static BufferedReader INTP  
  Utstream2bufferedreader (InputStream inputstream) {return new BufferedReader (new InputStreamReader (InputStream));

 }
}

Code Analysis:

When you convert JSON to objects, the code above gets the input stream from the server side, encapsulates the input stream as a BufferedReader object, and then converts the JSON to a Java object through the Fromjson () method.

The first parameter of the Gson Fromjson () method supports parameters of string, jsonelement, and reader types, and can be selected as needed. The second parameter of the Fromjson () method supports parameters of type and class<?> types, and you can use the class<?> parameter when you convert JSON to JavaBean. That is to use the corresponding JavaBean JAVABEAN.CALSS as the second parameter. When you need to turn JSON into a list generic, the map generics need to be typetoken to convert the second parameter to type (TypeToken is the data type converter provided by Gson and can support various data collection type conversions).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.