不同Json api生成json數據格式的區別

   

SentenceService sentenceService = new SentenceServiceImpl();

List<Sentence> sentences = sentenceService.sync("32", Timestamp.valueOf("2016-04-24 14:59:25"));
Gson gson = new Gson();

//使用Gson生成Json格式數據:
Map<String, Object> map = new HashedMap();
map.put("gson.toJson", sentences);
System.out.println(gson.toJson(map));

//使用org.json.JSONObject生成Json格式數據
org.json.JSONObject orgJson= new org.json.JSONObject();
orgJson.put("org.json.JSONObject", sentences);
System.out.println(orgJson.toString());

//使用net.sf.json.JSONObject生成Json格式數據
JSONObject netSfJson = new JSONObject();
netSfJson.put("net.sf.JSONObject", sentences);

System.out.println(netSfJson.toString());


代碼運行結果顯示:

Gson:

{"gson.toJson":[{"id":"SC100001","bookname":"暗時間","sentence":"人生質量並不是由活着的時間決定,而是由活着的時間乘以效率","mycomment":"說得好,令人深思!","userId":"32","createtime":"2016-04-24 15:00:57.0","modifytime":"2016-04-24 15:00:57.0","timestamp":"time_stamp","lastOperate":"c"}...

org.json:

{"org.json.JSONObject":[{"createtime":"2016-04-24 15:00:57.0","modifytime":"2016-04-24 15:00:57.0","timestamp":"time_stamp","id":"SC100001","userId":"32","sentence":"人生質量並不是由活着的時間決定,而是由活着的時間乘以效率","myComment":"說得好,令人深思!","lastOperate":"c","bookName":"暗時間"},{"createtime":"2016-04-24 15:23:49.0","modifytime":"2016-04-24 15:23:49.0","timestamp":"time_stamp","id":"SC1001d0021","userId":"32","sentence":"人生質量並不是由活着的時間決定,而是由活着的時間乘以效率","myComment":"說得好,令人深思!","lastOperate":"c","bookName":"暗時間"}...


net.sf:

{"net.sf.JSONObject":[{"bookName":"暗時間","createtime":"2016-04-24 15:00:57.0","id":"SC100001","lastOperate":"c","modifytime":"2016-04-24 15:00:57.0","myComment":"說得好,令人深思!","sentence":"人生質量並不是由活着的時間決定,而是由活着的時間乘以效率","timestamp":"time_stamp","userId":"32"},{"bookName":"暗時間","createtime":"2016-04-24 15:23:49.0","id":"SC1001d0021","lastOperate":"c","modifytime":"2016-04-24 15:23:49.0","myComment":"說得好,令人深思!","sentence":"人生質量並不是由活着的時間決定,而是由活着的時間乘以效率","timestamp":"time_stamp","userId":"32"}...


由上面可以看出,不同的Json Api生成對象的json格式數據也是不想同的,其中Gson生成的都是小寫,而org.json和net.sf兩個api生成的key有些是大寫 的,並且兩者相同,但各個節點的排序是不同的。


下面爲sentence類的代碼:

/**
 * 語錄實體
*/
public class Sentence implements Serializable, Cloneable{
private static final long serialVersionUID = 1L;
public static final String OPERATE_CREATE = "c";
public static final String OPERATE_MODIFY = "m";
public static final String OPERATE_DELETE = "d";
private String id;
private String bookname;
private String sentence;
private String mycomment;
private String userId;
private String createtime;
private String modifytime;
private String timestamp;
/**
* 該語錄最新一次修改類型:1,新增(c);2,修改(m);3,刪除(d)。注:若爲刪除,則返回的語錄只含id和lastOperate兩個字段
*/
private String lastOperate;


public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
public void setBookName(String bookname){
this.bookname=bookname;
}
public String getBookName(){
return bookname;
}
public void setSentence(String sentence){
this.sentence=sentence;
}
public String getSentence(){
return sentence;
}
public void setMyComment(String mycomment){
this.mycomment=mycomment;
}
public String getMyComment(){
return mycomment;
}
public void setUserId(String userId){
this.userId=userId;
}
public String getUserId(){
return userId;
}

public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getModifytime() {
return modifytime;
}
public void setModifytime(String modifytime) {
this.modifytime = modifytime;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getLastOperate() {
return lastOperate;
}
public void setLastOperate(String lastOperate) {
this.lastOperate = lastOperate;
}
@Override
public Sentence clone()  {
try {
return (Sentence) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
}







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