ajax请求json数据案例详解,JavaScript管理分析JSON数据经过详解

明日有诸如此类三个需要,点击四个六上,出现对应的有的呼吁消息,映今后底下,请求请求过后,第叁次点击就不必请求。
如图所示:点击欧洲上边现身请求的一些数额

复制代码 代码如下:

ajax请求json数据案例详解,ajaxjson案例详解

前几日有这么1个供给,点击多个6上,出现对应的一些伸手音信,展示在上面,请求请求过后,第1次点击就不要求请求。
如图所示:点击澳洲上边出现请求的片段多少

澳门葡京 1

html代码结构:

<div class="conSixmap">
  <div class="name conmap01" data-name="beimeizhou">
    <a href="javascript:void(0)">北美洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap02" data-name="nanmeizhou">
    <a href="javascript:void(0)">南美洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap03" data-name="ouzhou">
    <a href="javascript:void(0)">欧洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap04" data-name="feizhou">
    <a href="javascript:void(0)">非洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap05" data-name="yazhou">
    <a href="javascript:void(0)">亚洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap06" data-name="dayangzhou">
    <a href="javascript:void(0)">大洋洲</a>
    <div class="condetail"></div>
  </div>
</div> 

css样式:

.conSixmap{position:relative;width:678px;height:335px;margin:0 auto;background:url(../images/tuanduimapBg.png) no-repeat;color:#000;font-family:"微软雅黑"}
.conSixmap .name .condetail{display:none;position:absolute;z-index:10;width:216px;padding:10px;left:50%;margin-left:-118px;top:54px;background:url(../images/opcity83.png) repeat;border-radius:5px;}
.conSixmap .condetail span{display:block;color:#fff;font-size:14px;text-align:left;}
.conSixmap .name{position:absolute;width:52px;height:55px;}
.conSixmap .name a{display:block;z-index:2;position:absolute;padding-top:35px;text-align:center;cursor:pointer;width:52px;height:20px;color:#000;font-size:12px;}
.conSixmap .conmap01{left:91px;top:73px;}
.conSixmap .conmap01 a{background:url(../images/beimeipicBg.png) no-repeat top center;}
.conSixmap .conmap02 {left:180px;top:213px;}
.conSixmap .conmap02 a{background:url(../images/nanmeimapbg.png) no-repeat top center;}
.conSixmap .conmap03 {left:339px;top:68px;}
.conSixmap .conmap03 a{background:url(../images/ouzhoumapBg.png) no-repeat top center;}
.conSixmap .conmap04{left:327px;top:158px;}
.conSixmap .conmap04 a{background:url(../images/feizhoumapbg.png) no-repeat top center;}
.conSixmap .conmap05 {left:480px;top:75px;}
.conSixmap .conmap05 a{background:url(../images/yazhoumapBg.png) no-repeat top center;}
.conSixmap .conmap06 {left:545px;top:220px;}
.conSixmap .conmap06 a{background:url(../images/dayangmapbg.png) no-repeat top center;}

json格式:

{
  "beimeizhou": [
    "请求的json数据1",
    "请求的json数据2"
  ],
  "nanmeizhou": [
    "请求的json数据3",
    "请求的json数据4"
  ],
  "ouzhou": [
    "请求的json数据5",
    "请求的json数据6",
    "请求的json数据7",
    "请求的json数据8"
  ],
  "feizhou": [
    "请求的json数据9",
    "请求的json数据10",
    "请求的json数据11",
    "请求的json数据12"
  ],
  "yazhou": [
    "请求的json数据13",
    "请求的json数据14"
  ],
  "dayangzhou": [
    "请求的json数据15",
    "请求的json数据16"
  ]
}

js代码:

$(document).ready(function(){
  //添加地图
  var stauteArr={
      'beimeizhou':'true',
      'nanmeizhou':'true',
      'ouzhou':'true',
      'feizhou':'true',
      'yazhou':'true',
      'dayangzhou':'true'
    };
  $(".conSixmap .name").on('click',function(){
    var _this=this;
    var htmlcon="";
    $(this).siblings(".name").children(".condetail").fadeOut(500);
    $(this).children(".condetail").fadeIn(500);
    var _name=$(this).attr('data-name');
     $.ajax({
      url:"js/schoolMap.json",
      type:'get',
      data:{},
      dataType:"json",
      success: function(data){
        for(var i in data){
          if(_name==i && stauteArr[i]=='true'){
            for(var j=0;j<data[i].length;j++){
               htmlcon+=""+data[i][j]+"";
            }
            $(_this).children(".condetail").append(htmlcon);
            stauteArr[i]='false';
          }
        }
      },
      error: function(){
        alert('请求失败,请检查网络');
      }
    });
  });
});

后天有如此二个需要,点击多少个陆上,现身对应的有的呼吁音讯,呈现在底下,请求请求过后,…

ajax请求json数据案例详解,JavaScript管理分析JSON数据经过详解。JSON (JavaScript Object Notation)1种简单的多少格式,比xml更轻便。
JSON 是 JavaScript 原生格式,这意味在 JavaScript 中拍卖 JSON
数据没有须要别的异样的 API 或工具包。

澳门葡京 2

<script type=”text/javascript”>
function pageIndexChanged() {
var url = “/OrderList/Lists”;
var where = “”;
@*location.href = “@this.Url.Action(“List”)?pageindex=” +
$(“#pager”).pager(“pageindex”) + “&pagesize=” +
$(“#pager”).pager(“pagesize”) + “&where=” + escape(where);*@
var pageindex = $(“#pager”).pager(“pageindex”);
var pagesize = $(“#pager”).pager(“pagesize”);
where = escape(where);
$.getJSON(url, { where: where, pageindex: pageindex, pagesize: pagesize
}, function (data) {

JSON的规则很简短:
对象是三个冬季的“‘名称/值’对”集结。四个对象以“{”(左括号)起始,“}”(右括号)截止。各类“名称”后跟一个“:”(冒号);“‘名称/值’
对”之间采取“,”(逗号)分隔。具体细节参照他事他说加以考查

html代码结构:

alert(data.total);
alert(data.rows[0].Order.OrderID);

举个大约的例证:

<div class="conSixmap">
  <div class="name conmap01" data-name="beimeizhou">
    <a href="javascript:void(0)">北美洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap02" data-name="nanmeizhou">
    <a href="javascript:void(0)">南美洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap03" data-name="ouzhou">
    <a href="javascript:void(0)">欧洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap04" data-name="feizhou">
    <a href="javascript:void(0)">非洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap05" data-name="yazhou">
    <a href="javascript:void(0)">亚洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap06" data-name="dayangzhou">
    <a href="javascript:void(0)">大洋洲</a>
    <div class="condetail"></div>
  </div>
</div> 

alert(data.PageIndex);
alert(data.PageSize);

js 代码

css样式:

});
}

function showJSON() {  
  var user =  
  {  
  "username":"andy",  
  "age":20,  
  "info": { "tel": "123456", "cellphone": "98765"},  
  "address":  
  [  
  {"city":"beijing","postcode":"222333"},  
  {"city":"newyork","postcode":"555666"}  
  ]  
  }  
  alert(user.username);  
  alert(user.age);  
  alert(user.info.cellphone);  
  alert(user.address[0].city);  
  alert(user.address[0].postcode);  
  }
.conSixmap{position:relative;width:678px;height:335px;margin:0 auto;background:url(../images/tuanduimapBg.png) no-repeat;color:#000;font-family:"微软雅黑"}
.conSixmap .name .condetail{display:none;position:absolute;z-index:10;width:216px;padding:10px;left:50%;margin-left:-118px;top:54px;background:url(../images/opcity83.png) repeat;border-radius:5px;}
.conSixmap .condetail span{display:block;color:#fff;font-size:14px;text-align:left;}
.conSixmap .name{position:absolute;width:52px;height:55px;}
.conSixmap .name a{display:block;z-index:2;position:absolute;padding-top:35px;text-align:center;cursor:pointer;width:52px;height:20px;color:#000;font-size:12px;}
.conSixmap .conmap01{left:91px;top:73px;}
.conSixmap .conmap01 a{background:url(../images/beimeipicBg.png) no-repeat top center;}
.conSixmap .conmap02 {left:180px;top:213px;}
.conSixmap .conmap02 a{background:url(../images/nanmeimapbg.png) no-repeat top center;}
.conSixmap .conmap03 {left:339px;top:68px;}
.conSixmap .conmap03 a{background:url(../images/ouzhoumapBg.png) no-repeat top center;}
.conSixmap .conmap04{left:327px;top:158px;}
.conSixmap .conmap04 a{background:url(../images/feizhoumapbg.png) no-repeat top center;}
.conSixmap .conmap05 {left:480px;top:75px;}
.conSixmap .conmap05 a{background:url(../images/yazhoumapBg.png) no-repeat top center;}
.conSixmap .conmap06 {left:545px;top:220px;}
.conSixmap .conmap06 a{background:url(../images/dayangmapbg.png) no-repeat top center;}

$(function () {
$(“#order-details-search”).click(function () {
$(“.detail-search”).show(100);
});

 那意味二个user对象,具有username, age, info, address 等品质。

json格式:

$(“#澳门葡京,detail_search_cancle”).click(function () {
$(“.detail-search”).hide(100);
})
});
</script>

同1也得以用JSON来大约的改变数据,修改上边的事例

{
  "beimeizhou": [
    "请求的json数据1",
    "请求的json数据2"
  ],
  "nanmeizhou": [
    "请求的json数据3",
    "请求的json数据4"
  ],
  "ouzhou": [
    "请求的json数据5",
    "请求的json数据6",
    "请求的json数据7",
    "请求的json数据8"
  ],
  "feizhou": [
    "请求的json数据9",
    "请求的json数据10",
    "请求的json数据11",
    "请求的json数据12"
  ],
  "yazhou": [
    "请求的json数据13",
    "请求的json数据14"
  ],
  "dayangzhou": [
    "请求的json数据15",
    "请求的json数据16"
  ]
}

你或者感兴趣的稿子:

  • php管理json格式数据优良案例总计
  • AngularJs消除跨域难题案例详解(轻巧方法)
  • BootStrap的JS插件之轮播效果案例详解
  • JSP自定义标签案例分析
  • AngularJS完结要素展现和隐藏的多少个案例
  • JavaScript多图片上传案例
  • ajax请求json数据案例详解
  • JS案例分享之金额小写转大写
  • js对象转json数组的简易完结案例
  • javascript的接头及卓绝案例深入分析

js 代码

js代码:

function showJSON() {  
  var user =  
  {  
  "username":"andy",  
  "age":20,  
  "info": { "tel": "123456", "cellphone": "98765"},  
  "address":  
  [  
  {"city":"beijing","postcode":"222333"},  
  {"city":"newyork","postcode":"555666"}  
  ]  
  }  
  alert(user.username);  
  alert(user.age);  
  alert(user.info.cellphone);  
  alert(user.address[0].city);  
  alert(user.address[0].postcode);  
  user.username = "Tom";  
  alert(user.username);  
  } 
$(document).ready(function(){
  //添加地图
  var stauteArr={
      'beimeizhou':'true',
      'nanmeizhou':'true',
      'ouzhou':'true',
      'feizhou':'true',
      'yazhou':'true',
      'dayangzhou':'true'
    };
  $(".conSixmap .name").on('click',function(){
    var _this=this;
    var htmlcon="";
    $(this).siblings(".name").children(".condetail").fadeOut(500);
    $(this).children(".condetail").fadeIn(500);
    var _name=$(this).attr('data-name');
     $.ajax({
      url:"js/schoolMap.json",
      type:'get',
      data:{},
      dataType:"json",
      success: function(data){
        for(var i in data){
          if(_name==i && stauteArr[i]=='true'){
            for(var j=0;j<data[i].length;j++){
               htmlcon+=""+data[i][j]+"";
            }
            $(_this).children(".condetail").append(htmlcon);
            stauteArr[i]='false';
          }
        }
      },
      error: function(){
        alert('请求失败,请检查网络');
      }
    });
  });
});

JSON提供了json.js包,下载
后,将其引进然后就足以简轻便单的选取object.toJSONString()转变来JSON数据。

你可能感兴趣的稿子:

  • php管理json格式数据杰出案例总计
  • AngularJs化解跨域难点案例详解(轻巧方法)
  • BootStrap的JS插件之轮播效果案例详解
  • JSP自定义标签案例深入分析
  • AngularJS落成要素突显和隐形的多少个案例
  • JavaScript多图片上传案例
  • JS案例分享之金额小写转大写
  • js对象转json数组的简约实现案例
  • Ajax $.getJSON案例详解
  • javascript的掌握及优秀案例深入分析

js 代码

function showCar() {  
  var carr = new Car("Dodge", "Coronet R/T", 1968, "yellow");  
  alert(carr.toJSONString());  
  }  

  function Car(make, model, year, color)    {  
  this.make  =  make;  
  this.model  =  model;  
  this.year  =  year;  
  this.color  =  color;  
  }

能够选择eval来调换JSON字符到Object

js 代码

function myEval() {  
  var str = '{ "name": "Violet", "occupation": "character" }';  
  var obj = eval('(' + str + ')');  
  alert(obj.toJSONString());  
  }

抑或使用parseJSON()方法

js 代码

function myEval() {  
  var str = '{ "name": "Violet", "occupation": "character" }';  
  var obj = str.parseJSON();  
  alert(obj.toJSONString());  
  }

上边选取prototype写2个JSON的ajax例子。

先写1个servlet (我的是servlet.ajax.JSONTest1.java)就写一句话

java 代码

response.getWriter().print("{ \"name\": \"Violet\", \"occupation\": \"character\" }");  

再在页面中写多少个ajax的呼吁

js 代码

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-壹.5.1.js中提供了JSON的不二秘籍,String.evalJSON(),
能够不使用json.js, 修改上边的格局

js 代码

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

JSON还提供了java的jar包
API也非常粗略,上面举个例证

在javascript中填加请求参数

js 代码

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)

java 代码

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\" }");  
  }  

同1能够使用JSONObject生成JSON字符串,修改servlet

java 代码

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());  
  }  
  js 代码
  function jsonResponse(originalRequest) {  
  alert(originalRequest.responseText);  
  var myobj = originalRequest.responseText.evalJSON(true);  
  alert(myobj.name);  
  alert(myobj.age);  
  } 

上述内容便是给大家介绍JavaScrip管理t深入分析JSON数据经过详解,希望对我们具备帮衬。

您或然感兴趣的篇章:

  • JS中Json数据的拍卖和平化解析JSON数据的法子详解
  • JS
    对java重回的json格式的数额管理办法
  • JavaScript中深入分析JSON数据的三种方法
  • JavaScript深入分析JSON格式数据的不2法门言传身教
  • JS分析后台再次来到的JSON格式数据实例

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website