码迷,mamicode.com
首页 > 移动开发 > 详细

Android JSON 解析库的使用 - Gson 和 fast-json

时间:2015-08-10 23:43:10      阅读:653      评论:0      收藏:0      [点我收藏+]

标签:

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。
JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C、C++、C#、Java、JavaScript、Perl、Python等)。
这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成(网络传输速率)。

 

GSON是由谷歌官方推出的 JSON 与 Java 对象转化的 Java类库

fast-json 阿里推出的用于Android平台的库,性能很好的Java实现的JSON 解析和生成器,据说功能强大且速度超越现有的所有库

 

GSON用法:

1. 解析JSONObject,JSONArray

2. 将JSON实体转化为JSON数据

工程导入三个包 fastjson, gson,volley


//从豆瓣网获取数据, 在 http://www.bejson.com/ 解析查看数据
private void getData() {
    StringRequest request = new StringRequest(url, new Listener<String>() {
        @Override
        public void onResponse(String arg0) {
            Log.i(TAG, arg0); //获取完成后,在 http://www.bejson.com/ 解析查看数据
            dealData(arg0);
        }
    }, new ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError arg0) {
            // TODO Auto-generated method stub
        }
    });
    new Volley().newRequestQueue(getApplicationContext()).add(request);
}

//处理数据
private void dealData(String result){
    Gson gson = new Gson();
    Book book = gson.fromJson(result, Book.class); // data 转实体
    Log.i(TAG, book.getTitle() + "; "
            + book.getPublisher() + ";"
            + book.getTags().size() + ";" );
}

 

private String TAG = "CARLOZ";
private String url = "https://api.douban.com/v2/book/1220562";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jsonparse);
    
    getData(); 
}

Book属于实体类,详情,请查看代码GitHub

 

输出结果

08-10 21:19:22.321: I/CARLOZ(24911): {"rating":{"max":10,"numRaters":340,"average":"7.0","min":0},"subtitle":"","author":["[日] 片山恭一"],"pubdate":"2005-1","tags":[{"count":133,"name":"片山恭一","title":"片山恭一"},{"count":62,"name":"日本","title":"日本"},{"count":60,"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":"http:\/\/img3.douban.com\/mpic\/s1747553.jpg","binding":"平装","translator":["豫人"],"catalog":"\n      ","pages":"180","images":{"small":"http:\/\/img3.douban.com\/spic\/s1747553.jpg","large":"http:\/\/img3.douban.com\/lpic\/s1747553.jpg","medium":"http:\/\/img3.douban.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元"}
08-10 21:19:22.371: I/CARLOZ(24911): 满月之夜白鲸现; 青岛出版社;8;

 

Fast-json

public class FastJsonParseActivity extends Activity {
    
    private String TAG = "CARLOZ";
    private String url = "https://api.douban.com/v2/book/1220562";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fast_json_parse);
        
        getData();
    }

    private void getData() {
        StringRequest request = new StringRequest(url, new Listener<String>() {
            @Override
            public void onResponse(String arg0) {
                Log.i(TAG, arg0);
                dealData(arg0);
            }
        }, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                // TODO Auto-generated method stub
            }
        });
        new Volley().newRequestQueue(getApplicationContext()).add(request);
    }
    // JSON data to java object
    private void dealData(String result) {
        Book book = JSON.parseObject(result, Book.class);
        Log.i(TAG, book.getTitle() + "; "
                + book.getPublisher() + ";"
                + book.getTags().size() + ";" );
    }
}

 

运行结果:

08-10 21:43:11.991: I/CARLOZ(28779): {"rating":{"max":10,"numRaters":340,"average":"7.0","min":0},"subtitle":"","author":["[日] 片山恭一"],"pubdate":"2005-1","tags":[{"count":133,"name":"片山恭一","title":"片山恭一"},{"count":62,"name":"日本","title":"日本"},{"count":60,"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":"http:\/\/img3.douban.com\/mpic\/s1747553.jpg","binding":"平装","translator":["豫人"],"catalog":"\n      ","pages":"180","images":{"small":"http:\/\/img3.douban.com\/spic\/s1747553.jpg","large":"http:\/\/img3.douban.com\/lpic\/s1747553.jpg","medium":"http:\/\/img3.douban.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元"}
08-10 21:43:12.101: I/CARLOZ(28779): 满月之夜白鲸现; 青岛出版社;8;
// object to JSON data
private
void dealData(String result) { List<Book> books = JSON.parseObject(result, new TypeReference<List<Book>>(){}); Book book1 = new Book(); book1.setTitle("Hello CarloZ"); Book book2 = new Book(); book2.setTitle("Hello CarloZ"); Book book3 = new Book(); book3.setTitle("Hello CarloZ"); JSON.toJSON(book1); //book1 to JSON data JSON.toJSON(books); }

 

Android JSON 解析库的使用 - Gson 和 fast-json

标签:

原文地址:http://www.cnblogs.com/carlo/p/4719290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!