标签:
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/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; } }
结果为:
标签:
原文地址:http://blog.csdn.net/a249900679/article/details/51385913