Summary of the use of Android Gson

Source: Internet
Author: User

1. Concept

Gson is a JSON parsing framework released by Google

2. How to obtain

Github:https://github.com/google/gson

Android Studio uses

Compile ' com.google.code.gson:gson:2.7 '

View the latest version number

Download the latest jar package, http://search.maven.org/#search%7cga%7c1%7ccom.google.code.gson

3. Simple tool class based on Gson package Jsonutil
Package Com.zyj.app.util;import Com.google.gson.gson;import Com.google.gson.jsonarray;import Com.google.gson.jsonelement;import Com.google.gson.jsonparser;import Java.util.arraylist;import java.util.List;/* * * Created by ${zyj} on 2016/8/24.    */public class Jsonuitl {private static Gson Mgson = new Gson (); /** * Converts a JSON string into a solid object * @param JSON * @param classoft * @return */public static object Stringtoobj    ECT (String json, Class classoft) {return Mgson.fromjson (JSON, classoft); }/** * Convert the object to a JSON string or convert the list to JSON * @param object * @param <T> * @return * */Public St    atic <T> String objecttostring (T object) {return Mgson.tojson (object); /** * Convert JSON string to list * @param JSON * @param CLS * @param <T> * @return */public s        Tatic <T> list<t> stringtolist (String json,class<t> CLS) {Gson Gson = new Gson (); list<t> list = new Arraylist<t> ();        Jsonarray array = new Jsonparser (). Parse (JSON). Getasjsonarray ();        For (final jsonelement Elem:array) {List.add (Gson.fromjson (Elem, CLS));    } return list; }}
4. Simple test of Jsonutil

4.1. Defining Bean Objects

Package com.zyj.app.bean;/** * Created by ${zyj} on 2016/9/5. */public class Me {    String name;    String age;    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    Public String Getage () {        return age;    }    public void Setage (String age) {        this.age = age;    }}

4.2. Test code

        list<me> list = new arraylist<> ();        Me me1 = new Me ();        Me1.setname ("name111");        Me1.setage ("age111");        Me me2 = new Me ();        Me2.setname ("name222");        Me2.setage ("age222");        Me me3 = new Me ();        Me3.setname ("name333");        Me3.setage ("age333");        List.add (me1);        List.add (me2);        List.add (ME3);        Test one: Convert the list object to a JSON string        json1 = jsonuitl.objecttostring (list  );        Log print         klog.d ("Test One" + json1);       Test two: Convert the JSON string into a List set        list<me> listme = jsonuitl.stringtolist (Json1, me.class);        Log print for        (Me metest:listme) {            klog.d ("Test Two" + "Name:" + metest.getname () + "Age:" + metest.getage ()); c23/>//Test Three: Convert the object into a JSON string            jsonTest3 = jsonuitl.objecttostring (metest);            KLOG.D ("Test three" + JsonTest3  );        }

4.3. Test results

Test one [{' Age ': ' age111 ', ' name ': ' name111 '},{' age ': ' age222 ', ' name ': ' name222 '},{' age ': ' age333 ', ' name ': ' name333 '}] test two name:name111 age:age111 Test Three {"Age": "age111", "name": "name111"} Test two name:name222 age:age222 test three {"Age": "age222", "Name": " name222 "} Test two name:name333 age:age333 test three {" Age ":" age333 "," name ":" name333 "}

  

  

  

  

Summary of the use of Android Gson

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.