weixin_33733810 2016-12-05 07:41 采纳率: 0%
浏览 275

JSON解析响应缓慢

https://jsfiddle.net/jeeyi345/uchhjz7L/3/ I want to connect a JSP web service I built and return parsed elements, as follows, but the result return slowly.

When I parsed only 1 element the page response a lot faster, but I don't know why when I parsed them all, maybe like a few minutes or longer.

There is only a line of JSON array from the JSP page, like the following:

[{"UID":11,"UrlDate":"2016-10-24","UrlTime":"15:00:00","Drug":2,"AttLevel":43.2864439246683,"AttLevel_Lab":"普通","Allpoint":5,"PVTRuleBase":2,"MemLevel":87}]

Is there something wrong with my code? Thanks in adavance.

  // https://jsfiddle.net/vobmzdgr/7/
  function json_decode(json) {
      return $.parseJSON(json);
  }

  function json_format(json) {
      return JSON.stringify(json_decode(json), null, 2);
  }

  function json_highlight(json) {
      /*
      pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
      .string { color: green; }
      .number { color: darkorange; }
      .boolean { color: blue; }
      .null { color: magenta; }
      .key { color: red; }
      */
      if (typeof json != 'string') {
          json = JSON.stringify(json, undefined, 2);
      }
      json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
      return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
          var cls = 'number';
          if (/^"/.test(match)) {
              if (/:$/.test(match)) {
                  cls = 'color: red;'; //key
              } else {
                  cls = 'color: green;'; //string
              }
          } else if (/true|false/.test(match)) {
              cls = 'color: blue;'; //boolean
          } else if (/null/.test(match)) {
              cls = 'color: magenta;'; //null
          }
          return '<span style="' + cls + '">' + match + '</span>';
      });
  }

  function json_AttLevel(json) {
      var obj = json_decode(json);
      return obj[0].AttLevel;
  }

  function json_AttLevel_Lab(json) {
      var obj = json_decode(json);
      return obj[0].AttLevel_Lab; 
  }

  function json_PVTRuleBase(json) {
      var obj = json_decode(json);
      return obj[0].PVTRuleBase; 
  }

  function json_Drug(json) {
      var obj = json_decode(json);
      return obj[0].Drug;
  }

  function json_Allpoint(json) {
      var obj = json_decode(json);
      return obj[0].Allpoint; 
  }

  $(document).ready(function() {
      // var URL="http://140.138.77.151:8080/personalAssementNEW";
      var URL = "http://140.138.77.151:8080/FallRiskAPI/getInputData.jsp?ID=11&DATEINFO=2016-10-24&TIMEINFO=15:00:00";
      $.ajaxPrefilter(function(options) {
          if (options.crossDomain && jQuery.support.cors) {
              var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
              options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
          }
      });

      setInterval(function() {
          $.get(URL, function(response) {
              //原內容
              $("#output").text(response);
              var bodydata = response;
              bodydata = $("<p>" + bodydata + "</p>").text();

              var jdata = json_format(bodydata);
              $("#output_data").html("<pre>" + json_highlight(jdata) + "</pre>");

              $("#output_AttLevel").html("<pre>" + json_AttLevel(jdata) + "</pre>");

              $("#output_AttLevel_Lab").html("<pre>" + json_AttLevel_Lab(jdata) + "</pre>");

              $("#output_PVTRuleBase").html("<pre>" + json_PVTRuleBase(jdata) + "</pre>");

              $("#output_Drug").html("<pre>" + json_Drug(jdata) + "</pre>");

              $("#output_Allpoint").html("<pre>" + json_Allpoint(jdata) + "</pre>");
          });
          // alert("reloaded"); 
      }, 1500);

  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- https://jsfiddle.net/vobmzdgr/7/ -->
<script language="javascript" src="http://3wa.tw/inc/javascript/jquery/jquery-1.11.3.min.js"></script>
收到的內容:<br>
<div id="output"></div>
<hr>
解算的內容:<br>
<div id="output_data"></div>

專注力等級:<div id="output_AttLevel_Lab"></div>

專注力測驗等級:<div id="output_PVTRuleBase"></div>

用藥分數:<div id="output_Drug"></div>

整體分數:<div id="output_Allpoint"></div>

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
    • ¥15 模糊pid与pid仿真结果几乎一样
    • ¥15 java的GUI的运用
    • ¥15 Web.config连不上数据库
    • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
    • ¥15 怎么配置广告联盟瀑布流
    • ¥15 Rstudio 保存代码闪退
    • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
    • ¥50 invest生境质量模块