Gson轉換與JSONObject區別

韓師學子--胖佳發表於2019-03-10

                      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"}

 

相關文章