zoukankan      html  css  js  c++  java
  • 75、JSON 解析库---FastJson, Gson

    JSON 的简介: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。用于数据转换传输, 通用于PHP,Java,C++,C#,Python等编程语言数据交换传输。 易于人阅读和编写,同时也易于机器解析和生成。 基本结构:对象、数组。

    Gson 的简介和特点: Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。

    快速、高效

    代码量少、

    简洁 面向对象 数据传递和解析方便

    Gson 的基本用法:

    基本用法:定义实体类

                   根据需要可以将JSON生成单个实体或列表实体集合

    代码演示: 使用 Gson 解析 JsonObject

                   使用 Gson 解析 JsonArray

                  使用 Gson 将实体转为JSON数据

    1 public class Tag {
    2     private String count;
    3     private String name;
    4     private String title;
    6     。。。。。。。。
    7 }
    1 public class Book {
    2     private String title;
    3     private String publisher;
    4     private String summary;
    5     private ArrayList<Tag> tags;
    6     ...........................
    8 }
     1 /**
     2  * Gson的基本用法
     3  * https://api.douban.com/v2/book/1220562
     4  * @author Administrator
     5  */
     6 public class MainActivity extends Activity {
     7     private String url = "https://api.douban.com/v2/book/1220562";
     8 
     9     @Override
    10     protected void onCreate(Bundle savedInstanceState) {
    11         super.onCreate(savedInstanceState);
    12         setContentView(R.layout.activity_main);
    13         getData();
    14     }
    15 
    16     private void getData() {
    17         StringRequest request = new StringRequest(url, new Listener<String>() {
    18             @Override
    19             public void onResponse(String arg0) {
    20                 Log.i("info", arg0);
    21                 dealData(arg0);
    22             }
    23         }, new Response.ErrorListener() {
    24             @Override
    25             public void onErrorResponse(VolleyError arg0) {
    26                 // TODO Auto-generated method stub
    27             }
    28         });
    29         new Volley().newRequestQueue(getApplicationContext()).add(request);
    30     }
    31 
    32     private void dealData(String result) {
    33         Gson gson = new Gson();
    34         Book book = gson.fromJson(result, Book.class);
    35         Log.i("info", book.getTitle() + ":" + book.getPublisher() + ":"
    36                 + book.getTags().size());
    37     }
    38 }

    Fast-json 简介和特点: Fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。具有极快的性能,超越任其他的Java Json parser。

    快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson)

    强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)

    零依赖(没有依赖其它任何类库除了JDK)

    支持注解、支持全类型序列化

    Fast-json 的基本用法

    基本用法: 定义实体类

                   根据需要可以将JSON生成单个实体或列表实体集合

    代码演示: 使用 FastJson 解析 JsonObject

                   使用 FastJson 解析 JsonArray

                   使用 FastJson 将实体转为JSON数据

     1 public class Tag {
     2     private String title;
     3     private String name;
     4     private String count;
     5 
     6     public String getTitle() {
     7         return title;
     8     }
    10     public void setTitle(String title) {
    11         this.title = title;
    12     }
    14     public String getName() {
    15         return name;
    16     }
    18     public void setName(String name) {
    19         this.name = name;
    20     }
    22     public String getCount() {
    23         return count;
    24     }
    26     public void setCount(String count) {
    27         this.count = count;
    28     }
    30 }
     1 public class Book {
     2     private String title;
     3     private String publisher;
     4     private String summary;
     5     private ArrayList<Tag> tags;
     6 
     7     public String getTitle() {
     8         return title;
     9     }
    11     public void setTitle(String title) {
    12         this.title = title;
    13     }
    15     public String getPublisher() {
    16         return publisher;
    17     }
    19     public void setPublisher(String publisher) {
    20         this.publisher = publisher;
    21     }
    23     public String getSummary() {
    24         return summary;
    25     }
    27     public void setSummary(String summary) {
    28         this.summary = summary;
    29     }
    31     public ArrayList<Tag> getTags() {
    32         return tags;
    33     }
    35     public void setTags(ArrayList<Tag> tags) {
    36         this.tags = tags;
    37     }
    39 }
     1 /**
     2  * Fast-Json的基本用法
     3  * https://api.douban.com/v2/book/1220562
     4  * @author Administrator
     5  */
     6 public class MainActivity extends Activity {
     7     private String url = "https://api.douban.com/v2/book/1220562";
     8 
     9     @Override
    10     protected void onCreate(Bundle savedInstanceState) {
    11         super.onCreate(savedInstanceState);
    12         setContentView(R.layout.activity_main);
    13         getData();
    14     }
    15 
    16     private void getData() {
    17         StringRequest request = new StringRequest(url, new Listener<String>() {
    18             @Override
    19             public void onResponse(String arg0) {
    20                 Log.i("info", arg0);
    21                 dealData(arg0);
    22             }
    23         }, new Response.ErrorListener() {
    24             @Override
    25             public void onErrorResponse(VolleyError arg0) {
    26                 // TODO Auto-generated method stub
    27             }
    28         });
    29         Volley.newRequestQueue(getApplicationContext()).add(request);
    30     }
    31 
    32     private void dealData(String result) {
    33         Book book = JSON.parseObject(result, Book.class);
    34         List<Book> books = JSON.parseObject(result,
    35                 new TypeReference<List<Book>>() {
    36                 });
    37         Book book1 = new Book();
    38         book1.setTitle("biaoti");
    39         Book book2 = new Book();
    40         book2.setTitle("biaoti");
    41         Book book3 = new Book();
    42         book2.setTitle("biaoti");
    43 
    44         List<Book> list = new ArrayList<Book>();
    45         list.add(book1);
    46         list.add(book2);
    47         list.add(book3);
    48         JSON.toJSON(list);
    49         Log.i("info", book.getTitle() + ":" + book.getPublisher() + ":"
    50                 + book.getTags().size());
    51     }
    52 }
  • 相关阅读:
    HttpRunner接口自动化测试框架
    使用Appium 测试微信小程序和微信公众号方法
    WiFi无线连接真机进行Appium自动化测试方法
    idea tomcat 乱码问题的解决及相关设置
    解决idea导入maven项目缺少jar包的问题
    Docker php安装扩展步骤详解
    Python之No module named setuptools 安装pip
    MySQL中group_concat函数 --- 很有用的一个用来查询出所有group by 分组后所有 同组内的 内容
    Nginx如何来配置隐藏入口文件index.php(代码)
    vueThink框架搭建与填坑(new)
  • 原文地址:https://www.cnblogs.com/androidsj/p/5293130.html
Copyright © 2011-2022 走看看