简体   繁体   English

使用JQuery从json文件中按键或ID获取确切值

[英]Get Exact value by key or id from json file using JQuery

I have a json file containing with 50,000 user details . 我有一个包含50,000个用户详细信息的json文件。 i need to display the user details like name , mobile and address by using user id . 我需要通过使用用户ID显示用户详细信息,例如姓名,手机和地址 am done this with below code successfully but , it's taking too much time ( more than 15 seconds ) to get data and display on a html page. 成功使用以下代码完成了此操作, 但是 ,要花费太多时间( 超过15秒 )才能获取数据并显示在html页面上。

so, please help me to reduce the time consumption to fetch data from json file. 因此,请帮助我减少从json文件获取数据的时间。

html code is html代码是

<input type="text" id="search">
<input type="submit" id="submit">

i have json file (convertcsv.json) with below details 我有以下详细信息的json文件(convertcsv.json)

[
 {
   "id": 22313,
   "Name": "BABU",
   "Address": "Hyderabad"
 },
 {
  "id": 21314,
  "Name": "Raju",
  "Address": "BENGALURU"
 }
]

the actual JQuery script is 实际的JQuery脚本是

<script>
  $('#submit').on('click', function(){
    var searchField = $('#search').val();
    var regex = new RegExp(searchField, "i");
    var output = '<div class="row">';
    var count =0;
    $.getJSON('convertcsv.json', function(data) {
      $.each(data, function(key, val){
        if (val.id==searchField ) {
          output += '<div class="dataContain">';
          output += '<p><span style="font-weight:bold;"> id: </span>'+val.id+ '</p>';
          output += '<p><span style="font-weight:bold;"> Name: </span>'+val.Name + '</p>';
          output+='<p><span style="font-weight:bold;">Address: </span><span class="status">' + val.Address+'<span class="circle"></span></span></p>';
          output += '</div>';             
          count++;
        }


      });
     /// alert(count);
if(count==0){
output += 'No Records Found'
}
//alert(output)
      output += '</div>';
      //$('#results').toggle();
      $('#results').html(output);
    });
});

then the result is displayed in tag 然后结果显示在标签中

<div id="results"> Result Displayed here, i need to improve time, please help me, thanks in advance</div> 

Thank you. 谢谢。

 var json = [ { "id": 22313, "Name": "BABU", "Address": "Hyderabad" }, { "id": 21314, "Name": "Raju", "Address": "BENGALURU" } ] console.log(json.filter(x=> x.id == '22313')) 

Load your JSON file and then filter like above example, then create HTML from resulted JSON array. 加载您的JSON文件,然后像上面的示例一样进行过滤,然后从生成的JSON数组创建HTML。

EDIT 1 编辑1

$.getJSON('convertcsv.json', function(data) {

      // filter results given searchField then loop through from resulted JSON array
      // filter returns an array

      let result = data.filter(x=> x.id == searchField);
      $.each(result, function(key, val){

          output += '<div class="dataContain">';
          output += '<p><span style="font-weight:bold;"> id: </span>'+val.id+ '</p>';
          output += '<p><span style="font-weight:bold;"> Name: </span>'+val.Name + '</p>';
          output+='<p><span style="font-weight:bold;">Address: </span><span class="status">' + val.Address+'<span class="circle"></span></span></p>';
          output += '</div>';             
          count++;

        });


      });

EDIT 2 编辑2

Load JSON when document is ready and store into variable. 文档准备就绪时加载JSON并将其存储到变量中。

var jsonUsers;
$(document).ready(function () {
     $.getJSON('convertcsv.json', function(data) {
     jsonUsers = data;
  });
});

Now when click on button, filter out results from jsonUsers 现在,当单击按钮时,从jsonUsers过滤出结果

thank you very much @Ali Shahbaz , the final answer is., 非常感谢 @Ali Shahbaz, 最终答案是。,

var jsonUsers;
$(document).ready(function () {
   $.getJSON('convertcsv.json', function(data) {
     jsonUsers = data;
   });
});


  // filter results given searchField then loop through from resulted JSON array
  // filter returns an array

  let result = jsonUsers.filter(x=> x.id == searchField);
  $.each(result, function(key, val){

      output += '<div class="dataContain">';
      output += '<p><span style="font-weight:bold;"> id: </span>'+val.id+ '</p>';
      output += '<p><span style="font-weight:bold;"> Name: </span>'+val.Name + '</p>';
      output+='<p><span style="font-weight:bold;">Address: </span><span class="status">' + val.Address+'<span class="circle"></span></span></p>';
      output += '</div>';             
      count++;

    });

now it's working very well, without consuming loading time. 现在,它运行良好,而不会浪费加载时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM