Gson的基本介绍和使用
来源:互联网 发布:武汉华讯网络 编辑:程序博客网 时间:2024/06/04 17:56
JSON是我们在实际开发最常用的数据交换格式,GSON则是谷歌提供的用来解析Json数据的一个Java类库,在进行Gson介绍之前我们通过以下几个部分对Gson进行深入的了解。
一、JSON的简介
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。用于数据转化传输,通用于PHP、Java、C++、C#、Python等编程语言的数据交换传输。它易于人阅读和编写,同时也易于机器解析和生成。
基本结构:对象、数组
二、GSON简介和特点
GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可以将一个Json字符转成一个Java对象,或者将一个Java转化为Json字符串。
特点:a、快速、高效
b、代码量少、简洁
c、面向对象
d、数据传递和解析方便
三、GSON的基本使用
Json数据如下:
{ "rating": { "max": 10, "numRaters": 348, "average": "7.0", "min": 0 }, "subtitle": "", "author": [ "[日] 片山恭一" ], "pubdate": "2005-1", "tags": [ { "count": 136, "name": "片山恭一", "title": "片山恭一" }, { "count": 63, "name": "日本", "title": "日本" }, { "count": 61, "name": "日本文学", "title": "日本文学" }, { "count": 38, "name": "小说", "title": "小说" }, { "count": 32, "name": "满月之夜白鲸现", "title": "满月之夜白鲸现" }, { "count": 15, "name": "爱情", "title": "爱情" }, { "count": 8, "name": "純愛", "title": "純愛" }, { "count": 8, "name": "外国文学", "title": "外国文学" } ], "origin_title": "", "image": "https://img1.doubanio.com/mpic/s1747553.jpg", "binding": "平装", "translator": [ "豫人" ], "catalog": " ", "pages": "180", "images": { "small": "https://img1.doubanio.com/spic/s1747553.jpg", "large": "https://img1.doubanio.com/lpic/s1747553.jpg", "medium": "https://img1.doubanio.com/mpic/s1747553.jpg" }, "alt": "http://book.douban.com/subject/1220562/", "id": "1220562", "publisher": "青岛出版社", "isbn10": "7543632608", "isbn13": "9787543632608", "title": "满月之夜白鲸现", "url": "http://api.douban.com/v2/book/1220562", "alt_title": "", "author_intro": "", "summary": "那一年,是听莫扎特、钓鲈鱼和家庭破裂的一年。说到家庭破裂,母亲怪自己当初没有找到好男人,父亲则认为当时是被狐狸精迷住了眼,失常的是母亲,但出问题的是父亲……。", "price": "15.00元"}我们解析出其中的title、publisher、summary和数组tags。
1、创建一个android工程,并在AndroidManifest.xml文件上添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity类:
package com.example.gsondemo;import java.util.ArrayList;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.util.Log;import android.widget.Toast;import com.android.volley.Response.ErrorListener;import com.android.volley.Response.Listener;import com.android.volley.VolleyError;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import com.example.bean.Book;import com.example.bean.Tag;import com.google.gson.Gson;public class MainActivity extends ActionBarActivity{//请求接口private String url = "https://api.douban.com/v2/book/1220562";@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getData();}/** * 获取网络数据 */private void getData(){//创建请求对象StringRequest request = new StringRequest(url, new Listener<String>() {@Overridepublic void onResponse(String response){Log.e("info", response);dealData(response);}}, new ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error){Toast.makeText(MainActivity.this, "网络请求出错", Toast.LENGTH_SHORT).show();}});//把请求对象加入请求队列里面new Volley().newRequestQueue(getApplicationContext()).add(request);}public void dealData(String result){//实例化Gson对象Gson gson = new Gson();//把json字符串转化为对象Book book = gson.fromJson(result, Book.class);ArrayList<Tag> list = book.getTags();for (int i = 0; i < list.size(); i++){Tag tag = list.get(i);Log.e("name", tag.getName());}Toast.makeText(MainActivity.this, "标题:" + book.getTitle() + " 出版社:" + book.getPublisher()+ " Tag:" + book.getTags().size(), 1).show();}}
实体类Book:
package com.example.bean;import java.util.ArrayList;public class Book{private String title;//标题private String publisher;//出版社private String summary;//概要private ArrayList<Tag> tags;public String getTitle(){return title;}public void setTitle(String title){this.title = title;}public String getPublisher(){return publisher;}public void setPublisher(String publisher){this.publisher = publisher;}public String getSummary(){return summary;}public void setSummary(String summary){this.summary = summary;}public ArrayList<Tag> getTags(){return tags;}public void setTags(ArrayList<Tag> tags){this.tags = tags;}}Tag类:
package com.example.bean;public class Tag{private String count;private String name;private String title;public String getCount(){return count;}public void setCount(String count){this.count = count;}public String getName(){return name;}public void setName(String name){this.name = name;}public String getTitle(){return title;}public void setTitle(String title){this.title = title;}}
好了,GSON的使用就是那么简单。
项目下载地址:http://download.csdn.net/detail/u013043346/9405847
0 0
- Gson的基本介绍和使用
- Gson 的基本使用介绍
- Gson介绍和使用
- Gson的使用介绍
- GSON的使用介绍
- [学习小笔记] Gson的基本使用
- Jackson和Gson的使用
- Gson和Fastjson的使用
- Jackson和Gson的使用
- GSON和GsonFormat的使用
- GSON基本使用
- Gson基本使用
- Gson快速使用介绍
- Gson使用简单介绍
- FCKeditor的介绍和基本使用
- Lua的介绍和基本使用
- NSArray / NSDictionary 的基本介绍和使用
- Canvas的基本介绍和使用
- Linux命令学习手册-iconv命令
- maven项目管理
- Java程序员必备的IntelliJ IDEA插件
- Kconfig menuconfig .config Makefile之间的关系及如何在内核中添加驱动
- 个人笔记:table中加align="center"居中在chrome和火狐失效解决方法
- Gson的基本介绍和使用
- DIV浮动布局位置初步探索
- EJB正确客户端调用(jboss6、jboss7)
- Node.js_OS模块
- ExtJs布局详解
- 数据库的设计
- Web app root system property already set to different value
- sql:数据操作语言dml
- 调用百度地图,选取坐标接口,并转换成GPS坐标