感謝原文主,內容很詳細,很不錯
JSON是一種輕量級的數據交換格式。 易於人閱讀和編寫,同時也易於機器解析和生成(一般用於提升網絡傳輸速率)。
在之前我寫過一篇文章,《Android解析json數據》
http://blog.csdn.net/a249900679/article/details/51195505
介紹了json和用JSONObject、JSONArray解析json數據的方法,接下來幾篇文章將會介紹解析json數據的更好的方法:使用Gson解析。
Gson是Google推出的用來解析json數據以及將對象轉換成json數據的框架。可以很方便地實現json數據與對象的相互轉換,還可以自定義需要序列化或反序列化的字段。
使用Gson需要先導入jar,我這裏用到的是gson-2.3.1.jar
項目和jar下載地址:Github:https://github.com/smileysx/GsonTest
Oschina:https://git.oschina.net/ysx_xx/GsonText
詳解Gson使用(一)簡單對象轉化
http://blog.csdn.net/a249900679/article/details/51385913
詳解Gson使用(二)帶泛型的List轉化
http://blog.csdn.net/a249900679/article/details/51386028
詳解Gson使用(三)使用註解
http://blog.csdn.net/a249900679/article/details/51386509
http://blog.csdn.net/a249900679/article/details/51386660
詳解Gson使用(五)實現百度翻譯功能
http://blog.csdn.net/a249900679/article/details/51386727
其中gson-2.3.1.jar在項目lib目錄下
下面先來介紹簡單對象的轉換:
注意:以下所有實體類的變量名要跟json數據中的key相同
1.普通json數據對象實體類:
- public class ToJsonBeanOne {
- private int id;
- private String name;
- private int age;
- public ToJsonBeanOne(int id, String name, int age) {
- super();
- this.id = id;
- this.name = name;
- this.age = age;
- }
- public int getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- public void setId(int id) {
- this.id = id;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- String resultString = "";
- resultString += "id:" + id + "\nname:" + name + "\nage:" + age + "\n";
- return resultString;
- }
- }
可以看出來,上面對象中三個數據都是String類型,這是最簡單的。
看看如何把該對象序列化:
- public class ToJsonTest extends Activity {
- private TextView show;
- private Button start;
- private Gson gson;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initData();
- }
- private void initData() {
- gson = new Gson();
- show = (TextView) findViewById(R.id.showtext);
- start = (Button) findViewById(R.id.send);
- start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showData();
- }
- });
- }
- private void showData() {
- String resultString = "";
- resultString = one() + "\n\n";
- show.setText(resultString);
- }
- private String one() {
- //創建對象
- ToJsonBeanOne toJsonBeanOne = new ToJsonBeanOne(1, "小熊", 21);
- //將對象轉換爲json數據
- return gson.toJson(toJsonBeanOne);
- }
- }
結果爲:
接下來看看如何把json數據轉換爲該對象:
- public class FromJsonTest extends Activity {
- /**
- * 顯示數據的textview
- */
- private TextView show;
- /**
- * 按鈕
- */
- private Button start;
- /**
- * gson
- */
- private Gson gson;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initData();
- }
- private void initData() {
- gson = new Gson();
- show = (TextView) findViewById(R.id.showtext);
- start = (Button) findViewById(R.id.send);
- start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showData();
- }
- });
- }
- private void showData() {
- String showString = "";
- showString += one();
- show.setText(showString);
- }
- private String one() {
- //這裏創建個對象,是爲了得到json數據,實際中json數據可能是由網絡請求得到等
- ToJsonBeanOne toJsonBeanOne = new ToJsonBeanOne(1, "小熊", 21);
- String jsonString = gson.toJson(toJsonBeanOne);
- //將json數據轉換爲對象
- ToJsonBeanOne beanOne = gson.fromJson(jsonString, ToJsonBeanOne.class);
- String showString = "";
- showString += "json:" + jsonString + "\n解析結果爲:\n" + beanOne.toString();
- showString += "----------------------\n";
- return showString;
- }
- }
結果爲:
2.帶對象的對象實體類:
- public class ToJsonBeanTwo {
- private String school;
- private String classroom;
- private ToJsonBeanOne toJsonBeanOne;
- public ToJsonBeanTwo(String school, String classroom,
- ToJsonBeanOne toJsonBeanOne) {
- super();
- this.school = school;
- this.classroom = classroom;
- this.toJsonBeanOne = toJsonBeanOne;
- }
- public String getSchool() {
- return school;
- }
- public String getClassroom() {
- return classroom;
- }
- public ToJsonBeanOne getToJsonBeanOne() {
- return toJsonBeanOne;
- }
- public void setSchool(String school) {
- this.school = school;
- }
- public void setClassroom(String classroom) {
- this.classroom = classroom;
- }
- public void setToJsonBeanOne(ToJsonBeanOne toJsonBeanOne) {
- this.toJsonBeanOne = toJsonBeanOne;
- }
- @Override
- public String toString() {
- String resultString = "";
- resultString += "school:" + school + "\nclassroom:" + classroom
- + "\ntoJsonBeanOne:\nid:" + toJsonBeanOne.getId() + "\nname:"
- + toJsonBeanOne.getName() + "\nage:" + toJsonBeanOne.getAge()
- + "\n";
- return resultString;
- }
- }
可以看出該對象中不僅有String類型數據,還有ToJsonBeanOne對象數據。
看看如何把該對象序列化:
- public class ToJsonTest extends Activity {
- private TextView show;
- private Button start;
- private Gson gson;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initData();
- }
- private void initData() {
- gson = new Gson();
- show = (TextView) findViewById(R.id.showtext);
- start = (Button) findViewById(R.id.send);
- start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showData();
- }
- });
- }
- private void showData() {
- String resultString = "";
- resultString += three() + "\n\n";
- show.setText(resultString);
- }
- private String three() {
- ToJsonBeanTwo toJsonBeanTwo = new ToJsonBeanTwo("華軟", "軟工五班",
- new ToJsonBeanOne(1, "小熊", 21));
- return gson.toJson(toJsonBeanTwo);
- }
- }
結果爲:
接下來看看如何把json數據轉換爲該對象:
- public class FromJsonTest extends Activity {
- /**
- * 顯示數據的textview
- */
- private TextView show;
- /**
- * 按鈕
- */
- private Button start;
- /**
- * gson
- */
- private Gson gson;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initData();
- }
- private void initData() {
- gson = new Gson();
- show = (TextView) findViewById(R.id.showtext);
- start = (Button) findViewById(R.id.send);
- start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showData();
- }
- });
- }
- private void showData() {
- String showString = "";
- showString += three();
- show.setText(showString);
- }
- private String three() {
- ToJsonBeanTwo toJsonBeanTwo = new ToJsonBeanTwo("華軟", "軟工五班",
- new ToJsonBeanOne(1, "小熊", 21));
- String jsonString = gson.toJson(toJsonBeanTwo);
- ToJsonBeanTwo beanTwo = gson.fromJson(jsonString, ToJsonBeanTwo.class);
- String showString = "";
- showString += "json:" + jsonString + "\n解析後的數據:\n" + beanTwo.toString();
- showString += "----------------------\n";
- return showString;
- }
- }
結果爲:
3.既帶對象又帶List數據的對象(相當與json數據中有數組)實體類:
- public class ToJsonBeanThree {
- private String number;
- private ToJsonBeanTwo toJsonBeanTwo;
- private List<Book> books;
- public ToJsonBeanThree(String number, ToJsonBeanTwo toJsonBeanTwo,
- List<Book> books) {
- super();
- this.number = number;
- this.toJsonBeanTwo = toJsonBeanTwo;
- this.books = books;
- }
- public ToJsonBeanTwo getToJsonBeanTwo() {
- return toJsonBeanTwo;
- }
- public List<Book> getBooks() {
- return books;
- }
- public void setToJsonBeanTwo(ToJsonBeanTwo toJsonBeanTwo) {
- this.toJsonBeanTwo = toJsonBeanTwo;
- }
- public void setBooks(List<Book> books) {
- this.books = books;
- }
- public String getNumber() {
- return number;
- }
- public void setNumber(String number) {
- this.number = number;
- }
- @Override
- public String toString() {
- String resultString = "";
- resultString += "number:" + number + "\n";
- ToJsonBeanOne toJsonBeanOne = toJsonBeanTwo.getToJsonBeanOne();
- resultString += "toJsonBeanTwo:\nschool:" + toJsonBeanTwo.getSchool()
- + "\nclassroom:" + toJsonBeanTwo.getClassroom() + "\n";
- resultString += "toJsonBeanOne:\nid:" + toJsonBeanOne.getId()
- + "\nname:" + toJsonBeanOne.getName() + "\nage:"
- + toJsonBeanOne.getAge() + "\n";
- resultString += "books:\n";
- for (int i = 0; i < books.size(); ++i) {
- resultString += "bookName:" + books.get(i).getBookName()
- + "\nprice:" + books.get(i).getPrice() + "\n";
- }
- return resultString;
- }
- /**
- *
- * @ClassName: Book
- * @Description: 內部類
- * @author smile
- * @date 2016年5月12日 上午12:23:42
- *
- */
- public static class Book {
- private String bookName;
- private float price;
- public Book(String bookName, float price) {
- super();
- this.bookName = bookName;
- this.price = price;
- }
- public String getBookName() {
- return bookName;
- }
- public float getPrice() {
- return price;
- }
- public void setBookName(String bookName) {
- this.bookName = bookName;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- }
- }
可以看出該對象裏不僅有String類型,還有ToJsonBeanTwo對象類型,還有List<Book>類型,Book對象我寫成內部類,如果有多個類共用則寫成外部類,這裏要注意:寫成內部類要寫爲靜態內部類,不然解析會出錯。
看看如何把該對象序列化:
- public class ToJsonTest extends Activity {
- private TextView show;
- private Button start;
- private Gson gson;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initData();
- }
- private void initData() {
- gson = new Gson();
- show = (TextView) findViewById(R.id.showtext);
- start = (Button) findViewById(R.id.send);
- start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showData();
- }
- });
- }
- private void showData() {
- String resultString = "";
- resultString += five() + "\n\n";
- show.setText(resultString);
- }
- private String five() {
- ToJsonBeanTwo toJsonBeanTwo = new ToJsonBeanTwo("華軟", "軟工五班",
- new ToJsonBeanOne(1, "小熊", 21));
- List<Book> books = new ArrayList<Book>();
- for (int i = 1; i < 5; ++i) {
- books.add(new Book("第" + i + "本書", 25f * i));
- }
- ToJsonBeanThree toJsonBeanThree = new ToJsonBeanThree("1",
- toJsonBeanTwo, books);
- return gson.toJson(toJsonBeanThree);
- }
- }
結果爲:
可以看出對象中List數據books轉換成json數據變爲數組
接下來看看如何把json數據轉換爲該對象:
- public class FromJsonTest extends Activity {
- /**
- * 顯示數據的textview
- */
- private TextView show;
- /**
- * 按鈕
- */
- private Button start;
- /**
- * gson
- */
- private Gson gson;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initData();
- }
- private void initData() {
- gson = new Gson();
- show = (TextView) findViewById(R.id.showtext);
- start = (Button) findViewById(R.id.send);
- start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showData();
- }
- });
- }
- private void showData() {
- String showString = "";
- showString += five();
- show.setText(showString);
- }
- private String five() {
- ToJsonBeanTwo toJsonBeanTwo = new ToJsonBeanTwo("華軟", "軟工五班",
- new ToJsonBeanOne(1, "小熊", 21));
- List<Book> books = new ArrayList<Book>();
- for (int i = 1; i < 5; ++i) {
- books.add(new Book("第" + i + "本書", 25f * i));
- }
- ToJsonBeanThree toJsonBeanThree = new ToJsonBeanThree("1",
- toJsonBeanTwo, books);
- String jsonString = gson.toJson(toJsonBeanThree);
- ToJsonBeanThree beanThree = gson.fromJson(jsonString,
- ToJsonBeanThree.class);
- String showString = "";
- showString += "json:" + jsonString + "\n解析後的數據:\n"
- + beanThree.toString();
- showString += "----------------------\n";
- return showString;
- }
- }
結果爲: