import net.sf.json.JSONObject;
import com.google.gson.Gson;
public class Demo {
private int a;
private String b;
private String c;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
this.c= a+b;
return c;
}
public static void main(String[] args) {
Demo demo = new Demo();
demo.setA(1);
demo.setB("bbb");
Gson gson = new Gson();
System.out.println(gson.toJson(demo));
String jsonString = JSONObject.fromObject(demo).toString();
System.out.println(jsonString);
}
}
返回結果:
{"a":1,"b":"bbb"}
{"a":1,"b":"bbb","c":"1bbb"}
JSONObject在解析的過程中會對get方法進行解析,而Gson不會