java json序列换类

import com.google.gson.Gson;
 2 import com.google.gson.GsonBuilder;
 3
 4 public class Person {
 5     private String name;
 6     private int age;
 7     
 8     public Person(String name, int age) {
 9         this.name = name;
10         this.age = age;
11     }
12     
13     // getters and setters
14 }
15
16 public class JsonSerialize {
17     public static void main(String[] args) {
18         Gson gson = new GsonBuilder().create(); // 创建Gson对象
19         Person person = new Person("Tom", 18); // 创建Person对象
20         String json = gson.toJson(person); // 将Person对象转为Json字符串
21         System.out.println(json);
22     }
23 }

 

你可能感兴趣的