您的位置:首页 > Web前端 > JavaScript

Json 学习笔记

2011-05-18 12:14 120 查看
java后端处理

Json格式相对XML一种轻量级的返回结果。

如何将Java对象封装为Json格式,以下是一个Demo。

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

class Test{
public static void main(String [] args){
JSONObject obj = new JSONObject();
GroupKeeperJSON do1 = new GroupKeeperJSON();
do1.setqID("111");
GroupKeeperJSON do2 = new GroupKeeperJSON();
do2.setqName("name");
JSONArray array = new JSONArray();
array.add(do1);
array.add(do2);
obj.put("list", array);
Message msg = new Message(false);
msg.setMessage("OKK");
obj.put("info", msg);
System.out.println(obj);
}
}


当输出到页面时默认是text/html格式,对contextType需要修改为text/xml,如果需要进行编码,也需要对charset进行修改。

具体方法:

HttpServletResponse response = runData.getResponse();
response.setContentType("text/xml;charset=UTF-8");

response.getWrite().write(jsonStr);

alter table cps_group_keeper_relation drop constraint UK_CPS_GROUP_KEEPER_RELATION

前段页面处理

function joinAction(cID,qID,qName,type){
YUI().use('node','dialog','io',function(Y){
Y.io('${keeperModule}/market/cps_group_service.do',{
on: {
success: function(id,o){
eval("var s = " + o.responseText);//处理JSON返回结果
alert(s.msg);
}
},
method: "GET",
data: "cid="+cID+"&qid="+qID+"&act="+type+"&time=" + new Date().getTime()//加时间戳是为了防止多次打开无效
});
dialog_joinGroup.close();
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: