不同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;
}
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值