JSON

Ajax与JSON的使用(4)

字号+ 作者:H5之家 来源:H5之家 2015-11-19 12:00 我要评论( )

function sendRequest() { var url ="/MyWebApp/JSONTest1"; var mailAjax = newAjax.Request( url, { method: 'get', onComplete: jsonResponse } ); } function jsonResponse(originalRequest){ alert(originalRe


function sendRequest() {    
     var url = "/MyWebApp/JSONTest1";    
     var mailAjax = new Ajax.Request(    
url,    
{    
method: 'get',    
onComplete: jsonResponse    
}    
     );    
}    
function jsonResponse(originalRequest) {    
alert(originalRequest.responseText);    
     var myobj = originalRequest.responseText.parseJSON();    
alert(myobj.name);    
}    


prototype-1.5.1.js中提供了JSON的方法,String.evalJSON(), 可以不使用json.js, 修改上面的方法

程序代码


function jsonResponse(originalRequest) {    
alert(originalRequest.responseText);    
     var myobj = originalRequest.responseText.evalJSON(true);    
alert(myobj.name);    
}    


JSON还提供了java的jar包 API也很简单,下面举个例子

在javascript中填加请求参数

程序代码


function sendRequest() {    
     var carr = new Car("Dodge", "Coronet R/T", 1968, "yellow");    
     var pars = "car=" + carr.toJSONString();    
     var url = "/MyWebApp/JSONTest1";    
     var mailAjax = new Ajax.Request(    
url,    
{    
method: 'get',    
parameters: pars,    
onComplete: jsonResponse    
}    
     );    
}    


使用JSON请求字符串就可以简单的生成JSONObject并进行解析,修改servlet添加JSON的处理(要使用json.jar)

程序代码


private void doService(HttpServletRequest request, HttpServletResponse response) throws IOException {    
         String s3 = request.getParameter("car");    
         try {    
JSONObject jsonObj = new JSONObject(s3);    
System.out.println(jsonObj.getString("model"));    
System.out.println(jsonObj.getInt("year"));    
         } catch (JSONException e) {    
e.printStackTrace();    
}    
response.getWriter().print("{ \"name\": \"Violet\", \"occupation\": \"character\" }");    
     }    


同样可以使用JSONObject生成JSON字符串,修改servlet

程序代码


private void doService(HttpServletRequest request, HttpServletResponse response) throws IOException {    
         String s3 = request.getParameter("car");    
         try {    
JSONObject jsonObj = new JSONObject(s3);    
System.out.println(jsonObj.getString("model"));    
System.out.println(jsonObj.getInt("year"));    
         } catch (JSONException e) {    
e.printStackTrace();    
}    
         JSONObject resultJSON = new JSONObject();    
         try {    
resultJSON.append("name", "Violet")    
.append("occupation", "developer")    
.append("age", new Integer(22));    
System.out.println(resultJSON.toString());    
         } catch (JSONException e) {    
e.printStackTrace();    
}    
response.getWriter().print(resultJSON.toString());    
     }    


程序代码

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • php CI 实战教程:[5]用curl获取json并解析

    php CI 实战教程:[5]用curl获取json并解析

    2016-02-26 17:00

  • 浅谈使用PHP开发微信支付的流程

    浅谈使用PHP开发微信支付的流程

    2016-02-13 15:00

  • php json时间格式转换

    php json时间格式转换

    2016-02-08 11:48

  • json 在线转换php

    json 在线转换php

    2016-02-05 16:00

网友点评