Android Gson usage summary, androidgson Summary

Source: Internet
Author: User

Android Gson usage summary, androidgson Summary
1. Concepts

Gson is a json parsing framework released by Google.

2. How to obtain


Android studio

compile ''

View Latest Version

3. Gson-based simple tool class 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 (); /*** convert a json String to an Object * @ param json * @ param classOfT * @ return */public static Object stringToObject (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 static <T> String objectToString (T object) {return mGson. toJson (object );} /*** convert a json string to list * @ param json * @ param cls * @ param <T> * @ return */public static <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 define bean objects

package;/** * Created by ${zyj} on 2016/9/5. */public class Me {    String name ;    String age ;    public String getName() {        return name;    }    public void setName(String 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 1: Convert the list object to a json String json1 = JsonUitl. objectToString (list); // print the KLog. d ("Test 1" + json1); // Test 2: convert a json string to a list set List <Me> listMe = JsonUitl. stringToList (json1, Me. class); // log printing for (Me meTest: listMe) {KLog. d ("Test 2" + "name:" + meTest. getName () + "age:" + meTest. getAge (); // Test 3: Convert the object to a json String jsonTest3 = JsonUitl. objectToString (meTest); KLog. d ("Test 3" + jsonTest3 );}

4.3 Test Results

Test 1 [{"age": "age111", "name": "name111" },{ "age": "age222", "name": "name222 "}, {"age": "age333", "name": "name333"}] Test 2 name: name111 age: age111 Test 3 {"age": "age111 ", "name": "name111"} Test 2 name: name222 age: age222 Test 3 {"age": "age222", "name": "name222"} Test 2 name: name333 age: age333 Test 3 {"age": "age333", "name": "name333 "}









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: 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.