GSON是Google提供的用來在Java對象和JSON資料之間進行映射的Java類庫,GSON可以很容易的将一個java對象轉換成json文本,也可以很容易的将json文本轉換為java對象,兩種轉換方式分别對應的方法是:toJson()和fromJson()。
一:toJson()詳解(将一個java對象轉換成json文本)
1,生成生成數組類型的json文本,效果圖:
(點選按鈕 toJsonArray)

代碼:
首先建立一個類:GsonInfo;
public class GsonInfo {
private String name;
private int age;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "GsonInfo{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
public GsonInfo(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
}
layout布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_tojson"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="toJsonArray"/>
<Button
android:id="@+id/btn_fromjson"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fromJsonArray"/>
</LinearLayout>
<TextView
android:id="@+id/branch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/branch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_tojson2"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="toJsonMix"/>
<Button
android:id="@+id/btn_fromjson2"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fromJsonMix"/>
</LinearLayout>
<TextView
android:id="@+id/branch3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/branch4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/branch5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity代碼:
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.google.gson.Gson;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener{
private TextView tv_branch1;
private TextView tv_branch2;
private TextView tv_branch3;
private TextView tv_branch4;
private TextView tv_branch5;
private Gson gson = new Gson();//初始化Gson
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_tojson).setOnClickListener(this);
findViewById(R.id.btn_tojson2).setOnClickListener(this);
tv_branch1 = (TextView) findViewById(R.id.branch1);
tv_branch2 = (TextView) findViewById(R.id.branch2);
tv_branch3 = (TextView) findViewById(R.id.branch3);
tv_branch4 = (TextView) findViewById(R.id.branch4);
tv_branch5 = (TextView) findViewById(R.id.branch5);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_tojson:
toJson();//生成數組類型的json文本
break;
}
}
/**
* 格式類似于:
* [{
"name": "小琛",
"gender": "男",
"age": "21"
},
{
"name": "中琛",
"gender": "未知",
"age": "22"
},
{
"name": "大琛",
"gender": "男",
"age": "23"
}]
*/
private StringtoJson(){
List<GsonInfo> list = new ArrayList<GsonInfo>();
GsonInfo info1 = new GsonInfo("小琛",,"男");
list.add(info1);
GsonInfo info2 = new GsonInfo("中琛",,"未知");
list.add(info2);
GsonInfo info3 = new GsonInfo("大琛",,"男");
list.add(info3);
tv_branch1.setText(gson.toJson(list));
return gson.toJson(list);
}
}
2,一個對象嵌套一個對象的json文本,效果圖:
(點選按鈕 toJsonMix)
代碼:
除了 GsonInfo類,還要建立一個類:GsonInfo2:
public class GsonInfo2 {
private String boyFriend;
private String grilFriend;
public GsonInfo2() {
super();
}
public GsonInfo2(String boyFriend, String grilFriend) {
super();
this.boyFriend = boyFriend;
this.grilFriend = grilFriend;
}
public String getBoyFriend() {
return boyFriend;
}
public void setBoyFriend(String boyFriend) {
this.boyFriend = boyFriend;
}
public String getGrilFriend() {
return grilFriend;
}
public void setGrilFriend(String grilFriend) {
this.grilFriend = grilFriend;
}
@Override
public String toString() {
return "GsonInfo2{" +
"boyFriend='" + boyFriend + '\'' +
", grilFriend='" + grilFriend + '\'' +
'}';
}
}
布局不用修改,MainActivity:
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.google.gson.Gson;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener{
private TextView tv_branch1;
private TextView tv_branch2;
private TextView tv_branch3;
private TextView tv_branch4;
private TextView tv_branch5;
private Gson gson = new Gson();//初始化Gson
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_tojson).setOnClickListener(this);
findViewById(R.id.btn_fromjson).setOnClickListener(this);
findViewById(R.id.btn_tojson2).setOnClickListener(this);
findViewById(R.id.btn_fromjson2).setOnClickListener(this);
tv_branch1 = (TextView) findViewById(R.id.branch1);
tv_branch2 = (TextView) findViewById(R.id.branch2);
tv_branch3 = (TextView) findViewById(R.id.branch3);
tv_branch4 = (TextView) findViewById(R.id.branch4);
tv_branch5 = (TextView) findViewById(R.id.branch5);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_tojson2:
toJson2();//一個對象嵌套一個對象的json文本
break;
}
}
/**
* 格式類似于:
* {
"friend": {
"grilFriend": "小花",
"bogFriend": "小美"
},
"name": "小明",
"gender": "男",
"age": 11
}
*/
private void toJson2(){
GsonInfo info = new GsonInfo("小明",,"男");
GsonInfo2 info2 = new GsonInfo2("小美","小花");
String infoStr = gson.toJson(info);
String info2Str = gson.toJson(info2);
tv_branch3.setText(infoStr);
tv_branch4.setText(info2Str);
JSONObject object = null;
try {
JSONObject infoObject = new JSONObject(info2Str);
object = new JSONObject(infoStr);
object.put("friend",infoObject);//鍵值對
}catch (Exception e){
e.printStackTrace();
}
tv_branch5.setText(object.toString());
}
}
二:fromJson()詳解(将json文本轉換為java對象)
private void fromJson() {
//獲得toJson()中的JSON的字元串
String fromData = toJson();
Type type = new TypeToken<GsonInfo>() {
}.getType();
GsonInfo gsonInfo = new GsonInfo();
gsonInfo = gson.fromJson(fromData, type);
tv_branch2.setText(gsonInfo.toString());
}