有没有类似于 PHP 的 JSON_DECODE 函数的 Javascript 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14978498/
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-25 08:18:16  来源:igfitidea点击:

Is there any Javascript function similar to PHP's JSON_DECODE function?

phpjqueryajaxjson

提问by Rehan Adil

I'm using jQuery AJAX to load part of my web page. And my AJAX datatype is HTML. I've heard JSON is faster and I've used it too. But JSON doesn't seem to work when the data is a little big, for example:

我正在使用 jQuery AJAX 加载我的网页的一部分。我的 AJAX 数据类型是 HTML。我听说 JSON 更快,我也使用过它。但是当数据有点大时 JSON 似乎不起作用,例如:

It works when the data is short:

当数据很短时它起作用:

{"name" : "John Smith" , "age" : "32" , "status" : "married" }

{"name" : "Bella Gilbert" , "age" : "26" , "status" : "single" }

But not when the data is a little big:

但不是当数据有点大时:

{"name" : "John Smith" , "age" : "32" , "status" : "married" }

{"name" : "Bella Gilbert" , "age" : "26" , "status" : "single" }

{"name" : "Joseph Morgan" , "age" : "28" , "status" : "single" }

{"name" : "Paul Wesley" , "age" : "24" , "status" : "single" }

Is there any way I can just fetch the data without stating dataType as JSON and then decode it using javascript, as similar to PHP's function:

有什么方法可以只获取数据而不将 dataType 声明为 JSON,然后使用 javascript 对其进行解码,类似于 PHP 的功能:

json_decode($data);

json_decode($data);

Or if not then please suggest a way to handle large JSON data using jQuery AJAX. Thanks!

或者,如果没有,请提出一种使用 jQuery AJAX 处理大型 JSON 数据的方法。谢谢!

回答by Yogesh Suthar

use this

用这个

var obj = jQuery.parseJSON(json_data);

It will decode the json_data

它将解码 json_data

http://api.jquery.com/jQuery.parseJSON/

http://api.jquery.com/jQuery.parseJSON/

回答by Jai

use JSON.parse()to convert a JSON string to an object:

用于JSON.parse()将 JSON 字符串转换为对象:

var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
var contact = JSON.parse(jsontext);
document.write(contact.surname + ", " + contact.firstname);

// Output: Aaberg, Jesper

jquery version: (Parses a JSON string.)

jquery 版本: ( Parses a JSON string.)

var obj = jQuery.parseJSON('{"name":"John"}');
alert(obj.name);

回答by Darin Dimitrov

You could use the $.parseJSON()method to parse a JSON encoded string into the corresponding javascript object. But if you are performing an AJAX request to your server and the data is coming from it you don't need to use this method at all because jQuery will automatically parse the result passed to the success function:

您可以使用该$.parseJSON()方法将 JSON 编码的字符串解析为相应的 javascript 对象。但是如果你正在向你的服务器执行 AJAX 请求并且数据来自它,你根本不需要使用这个方法,因为 jQuery 会自动解析传递给成功函数的结果:

$.ajax({
    url: '/somescript.php',
    dataType: 'json',
    success: function(result) {
        // result is already a parsed javascript object that you could manipulate directly here
    }
});

And if you write your server side script properly so that it sets the response Content-TypeHTTP header to application/json(which you should always be doing anyways) you don't even need to indicate to jQuery the dataTypeparameter. jQuery will analyze this response header and automatically parse the result for you:

如果您正确编写服务器端脚本,以便将响应Content-TypeHTTP 标头设置为application/json(无论如何您应该始终这样做),您甚至不需要向 jQuery 指示dataType参数。jQuery 会分析这个响应头并自动为你解析结果:

$.ajax({
    url: '/somescript.php',
    success: function(result) {
        // result is already a parsed javascript object that you could manipulate directly here
    }
});

回答by Fab

the jQuery.parseJSONmethod can do this.

jQuery.parseJSON方法可以做到这一点。

回答by Sharon Haim Pour

Your json object is malformed. Should look like this:

您的 json 对象格式错误。应该是这样的:

[{"name" : "John Smith" , "age" : "32" , "status" : "married" },

{"name" : "Bella Gilbert" , "age" : "26" , "status" : "single" },

{"name" : "Joseph Morgan" , "age" : "28" , "status" : "single" },

{"name" : "Paul Wesley" , "age" : "24" , "status" : "single" }]  

Use thistool to check your object.

使用工具检查您的对象。