Gson轉換與JSONObject區別

package demo1;


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不會

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章