Gson简单使用

Google开源的一个将Java对象转成Json格式的项目Gson

项目Google Code:http://code.google.com/p/google-gson
API文档:http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html
用户指南:http://sites.google.com/site/gson/gson-user-guide

以下项目导入附加包
public class student {
private String name;
private String sex;
private int year;
public student(){
 this.name = "einverne";
 this.sex = "m";
 this.year = 12;
}
public student(String n,String s,int y){
  name = n;
  sex = s;
  year = y;
 }
}


import com.google.gson.Gson;
public class testmain {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  student student = new student();
  Gson gson = new Gson();
  System.out.println(gson.toJson(student));
  
  student student2 = new student("wewew","we",12);
  System.out.println(gson.toJson(student2));
  
  int[] ints = {1,2,3,4,5};
  String[] strings = {"abc","def","ghi"};
  System.out.println(gson.toJson(ints));
  System.out.println(gson.toJson(strings)); 
 }
}


Related Articles

Quote Of The Day