Gson转换与JSONObject区别
转载:https://blog.csdn.net/ccxcccx/article/details/65937139
net.sf.json.JSONObject
com.google.gson.Gson
JSONObject在解析的过程中会对get方法进行解析,而Gson不会
例子如下:
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"}