• 使用Gson送解析Json格式


    Java bean:

    package com.jingle.a;
    
    public class Person {
        public String name;
        public int age;
        
        public Person (){
            
        }
        public Person(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
        
        
    
    }

    测试类:

     1 package com.jingle.a;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import com.google.gson.Gson;
     7 import com.google.gson.reflect.TypeToken;
     8 
     9 public class A {
    10 
    11     /**
    12      * @param args
    13      */
    14     public static void main(String[] args) {
    15         // TODO Auto-generated method stub
    16 
    17         Gson gson = new Gson();
    18 
    19         List<Person> perList = new ArrayList<Person>();
    20         for (int i = 0; i < 5; i++) {
    21             Person p = new Person(String.valueOf(i), i);
    22             perList.add(p);
    23 
    24         }
    25         // 对象实体的序列化:将对象实体转为json格式
    26         String str = gson.toJson(perList);
    27 
    28         System.out.println(perList);
    29         // list toString 带类名称
    30         // [Person [name=0, age=0], Person [name=1, age=1], Person [name=2,
    31         // age=2], Person [name=3, age=3], Person [name=4, age=4]]
    32         System.out.println(str);
    33 
    34         // 使用Gson将list转为Json格式输出则不带类名,只是key-value
    35         // [{"name":"0","age":0},{"name":"1","age":1},{"name":"2","age":2},{"name":"3","age":3},{"name":"4","age":4}]
    36 
    37         // json格式的反序列化,将json格式转为对象实体
    38         // 在日常应用中,我们一般都会碰到两种情况,转成单一实体对象和转换成对象列表或者其他结构。
    39 
    40         String j1 = "{'name':'a', 'age':'25'}";
    41         Gson g1 = new Gson();
    42         // 单一实体对象
    43         // 参数1:json数据,参数2:需要转换对象的类型
    44         // **********************
    45         Person p1 = g1.fromJson(j1, Person.class);
    46         // **********************
    47 
    48         System.out.println(p1);
    49 
    50         // 对象列表
    51         Gson g2 = new Gson();
    52 
    53         // **********************
    54         List<Person> ps = g2.fromJson(str, new TypeToken<List<Person>>() {
    55         }.getType());
    56         // **可以看到上面的代码使用了TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换
    57         // gson和其他现有java
    58         // json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需要序列化的字段。
    59         System.out.println(ps);
    60 
    61     }
    62 }
  • 相关阅读:
    如何运行 PPAS上的pgpoolII
    Postmaster主循环的大致流程
    对ListenSocket 的研究(三)
    对ListenSocket 的研究(二)
    对ListenSocket 的研究(五)
    PostgreSQL的postmaser的fork的学习体会
    赛门铁克公告:解密Kneber恶意软件 狼人:
    微软免费杀毒软件MSE最新版本释出 狼人:
    Facebook出现邮件错发故障 隐私安全再受关注 狼人:
    McAfee和Brocade将联合开发网络安全解决方案 狼人:
  • 原文地址:https://www.cnblogs.com/jinglecode/p/4341100.html
Copyright © 2020-2023  润新知