为什么 jQuery.parseJSON 不是函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2535825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:44:03  来源:igfitidea点击:

Why is jQuery.parseJSON not a function?

jqueryjsonparsing

提问by ACP

I use the following jQuery statements and I am getting the error,

我使用以下 jQuery 语句,但出现错误,

jQuery.parseJSON is not a function

jQuery.parseJSON 不是函数

My function is,

我的功能是,

function Iteratejsondata() {var HfJsonValue = 
      { "Table": [{ "Emp_Id": "3", 
                    "Identity_No": "", 
                    "Emp_Name": "Jerome", 
                    "Address": "Madurai", 
                    "Date_Of_Birth": "", 
                    "Desig_Name": "Supervisior", 
                    "Desig_Description": "Supervisior of the Construction", 
                    "SalaryBasis": "Monthly", 
                    "FixedSalary": "25000.00" }, 
                  { "Emp_Id": "4", 
                    "Identity_No": "", 
                    "Emp_Name": "Mohan", 
                    "Address": "Madurai", 
                    "Date_Of_Birth": "", 
                    "Desig_Name": "Acc ", 
                    "Desig_Description": "Accountant", 
                    "SalaryBasis": "Monthly", 
                    "FixedSalary": "200.00" }, 
                  { "Emp_Id": "5", 
                    "Identity_No": "", 
                    "Emp_Name": "Murugan", 
                    "Address": "Madurai", 
                    "Date_Of_Birth": "", 
                    "Desig_Name": "Mason", 
                    "Desig_Description": "Mason", 
                    "SalaryBasis": "Weekly", 
                    "FixedSalary": "150.00" }, 
                  { "Emp_Id": "6", 
                    "Identity_No": "", 
                    "Emp_Name": "Ram", 
                    "Address": "Madurai", 
                    "Date_Of_Birth": ""
                     "Desig_Name": "Mason", 
                     "Desig_Description": "Mason", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "120.00" }, 
                   { "Emp_Id": "7", 
                     "Identity_No": "", 
                     "Emp_Name": "Raja", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Mason", 
                     "Desig_Description": "Mason", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "135.00" }, 
                   { "Emp_Id": "8", 
                     "Identity_No": "", 
                     "Emp_Name": "Raja kumar", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Mason Helper", 
                     "Desig_Description": "Mason Helper", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "105.00" }, 
                   { "Emp_Id": "9", 
                     "Identity_No": "", 
                     "Emp_Name": "Lakshmi", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Mason Helper", 
                     "Desig_Description": "Mason Helper", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "100.00" }, 
                   { "Emp_Id": "10", 
                     "Identity_No": "", 
                     "Emp_Name": "Palani", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Carpenter", 
                     "Desig_Description": "Carpenter", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "200.00" }, 
                   { "Emp_Id": "11", 
                     "Identity_No": "", 
                     "Emp_Name": "Annamalai", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Carpenter", 
                     "Desig_Description": "Carpenter", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "220.00" }, 
                   { "Emp_Id": "12", 
                     "Identity_No": "", 
                     "Emp_Name": "David", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Steel Fixer", 
                     "Desig_Description": "Steel Fixer", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "220.00" }, 
                   { "Emp_Id": "13", 
                     "Identity_No": "", 
                     "Emp_Name": "Chandru", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Steel Fixer", 
                     "Desig_Description": "Steel Fixer", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "220.00" }, 
                   { "Emp_Id": "14", 
                     "Identity_No": "", 
                     "Emp_Name": "Mani", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Steel Helper", 
                     "Desig_Description": "Steel Helper", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "175.00" }, 
                   { "Emp_Id": "15", 
                     "Identity_No": "", 
                     "Emp_Name": "Karthik", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Wood Fixer", 
                     "Desig_Description": "Wood Fixer", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "195.00" }, 
                   { "Emp_Id": "16", 
                     "Identity_No": "", 
                     "Emp_Name": "Bala", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Wood Fixer", 
                     "Desig_Description": "Wood Fixer", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "185.00" }, 
                   { "Emp_Id": "17", 
                     "Identity_No": "", 
                     "Emp_Name": "Tamil arasi", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Wood Helper", 
                     "Desig_Description": "Wood Helper", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "185.00" }, 
                   { "Emp_Id": "18", 
                     "Identity_No": "", 
                     "Emp_Name": "Perumal", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Cook", 
                     "Desig_Description": "Cook", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "105.00" }, 
                   { "Emp_Id": "19", 
                     "Identity_No": "", 
                     "Emp_Name": "Andiappan", 
                     "Address": "Madurai", 
                     "Date_Of_Birth": "", 
                     "Desig_Name": "Watchman", 
                     "Desig_Description": "Watchman", 
                     "SalaryBasis": "Weekly", 
                     "FixedSalary": "150.00"}]
      };

    //var jsonObj = eval('(' + HfJsonValue + ')');
    var jsonObj = jQuery.parseJSON(HfJsonValue);

and my page looks like this

我的页面看起来像这样

 <div id="Pagination" class="page-numbers"></div>
  <br style="clear:both;" />
  <div id="Searchresult"></div>

  <div id="hiddenresult" style="display:none;">

  </div>
 <script type="text/javascript">
     var pagination_options = {
         num_edge_entries: 2,
         num_display_entries: 8,
         callback: pageselectCallback,
         items_per_page: 3
     }
     function pageselectCallback(page_index, jq) {
         var items_per_page = pagination_options.items_per_page;
         var offset = page_index * items_per_page;
         var new_content = $('#hiddenresult div.resultsdiv').slice(offset, offset + items_per_page).clone();
         $('#Searchresult').empty().append(new_content);
         return false;
     }
     function initPagination() {
         var num_entries = $('#hiddenresult div.resultsdiv').length;
         // Create pagination element
         $("#Pagination").pagination(num_entries, pagination_options);
     }
     $(document).ready(function() {
         Iteratejsondata();
         initPagination();
     });
</script>

I have inspected through Firebug and saw all jQuery files have been downloaded but why is this happening?

我已经通过 Firebug 检查并看到所有 jQuery 文件都已下载,但为什么会发生这种情况?

回答by Yisroel

What version of jQuery are you using? parseJSONwas added in version 1.4.1, so if you're using earlier versions, it's not there.

您使用的是哪个版本的 jQuery?parseJSON是在 1.4.1 版中添加的,因此如果您使用的是早期版本,则它不存在。

回答by Rajat

You can also use JSON.parse(). Its natively supported on FF, Chrome,Safari and IE8 and is most likely going to be faster than the JQuery version.

您还可以使用 JSON.parse()。它原生支持 FF、Chrome、Safari 和 IE8,并且很可能比 JQuery 版本更快。

If you are targeting IE7 and IE6, JQuery would be the best bet.

如果您的目标是 IE7 和 IE6,JQuery 将是最好的选择。

回答by Jonathan

What version of jQuery are you using? parseJSON is only available from 1.4.1.

您使用的是哪个版本的 jQuery?parseJSON 仅从 1.4.1 开始可用。

Also, you can only pass a String to the function: http://api.jquery.com/jQuery.parseJSON/

此外,您只能将字符串传递给函数:http: //api.jquery.com/jQuery.parseJSON/

回答by Eric

In your example, you don't need to parse HfJsonValue, it's already embedded directly, it will be correctly handled by the browser.

在您的示例中,您不需要 parse HfJsonValue,它已经直接嵌入,浏览器将正确处理。

回答by dogatonic

I kept getting the same error, "jQuery.parseJSON not a function", until I updated to 1.4.4

我一直收到同样的错误,“jQuery.parseJSON not a function”,直到我更新到 1.4.4

|script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"|

|脚本类型="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"|

回答by adrind

I also ran into this error but it was due to an old javascript library I was using along with jQuery 3.

我也遇到了这个错误,但这是由于我与 jQuery 3 一起使用的旧 javascript 库。

As of jQuery 3+ jQuery.parseJson has been deprecated in favor of the native JSON.parse that has the same browser support as jQuery 3

从 jQuery 3+ 开始,jQuery.parseJson 已被弃用,取而代之的是与 jQuery 3 具有相同浏览器支持的原生 JSON.parse