当前位置:首页>开发>正文

json如何使用

2023-04-27 22:08:51 互联网 未知 开发

 json如何使用

json如何使用

json一般都是配合ajax一起使用的 我做做过的小例子 粘给你 你可以研究一下
js部分
//获取卡的金额
function get_money(){
var str=document.getElementById("pk_card_type").value
//alert(str)
var url = /member_h.do
var pars = method=getMoney
pars =&pk_card_type= str
var ajax = new Ajax.Request(
url,
{method:post,parameters:pars,onComplete:show_money}
)

}
//回调函数 写入卡的金额
function show_money(dataResponse)
{
var data = eval(( dataResponse.responseText ))
var price=0
price=data.price
var collection_fees=0
collection_fees=data.collection_fees
document.getElementById("recharge").value=price
document.getElementById("collection_fees").value=collection_fees
}

action部分
public ActionForward getMoney(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html charset=utf-8")

try {
IElementaryFileService ggsv = new ElementaryFileService()
String pk_card_type = request.getParameter("pk_card_type")
Card_TypeVO ctvo=new Card_TypeVO()
ctvo=ggsv.queryByPK(Card_TypeVO.class, pk_card_type)
PrintWriter out = response.getWriter()
// 这里的数据拼装一般是从数据库查询来的
JSONObject jsonObject = new JSONObject()
if(ctvo!=null){
jsonObject.put("price", ctvo.getCard_money())
jsonObject.put("collection_fees", ctvo.getCash())
}else{
jsonObject.put("price", 0)
jsonObject.put("collection_fees", 0)
}

out.print(jsonObject.toString())
out.flush()
out.close()
return null
} catch (Exception e) {
e.printStackTrace()
return null
}
}

最新文章

随便看看