用jquery解析JSON

用jquery解析JSON,jquery,json,api,Jquery,Json,Api,这里完全是初学者。我有jquery库。我调用了一个返回json的api。我想使用jquery库中的parseJSON函数来解析它。简单地说,我不知道怎么做 我可以在jquery库中找到该函数,它看起来是这样的: parseJSON: function( data ) { if ( typeof data !== "string" || !data ) { return null; } // Make sure leading/trailing white

这里完全是初学者。我有jquery库。我调用了一个返回json的api。我想使用jquery库中的parseJSON函数来解析它。简单地说,我不知道怎么做

我可以在jquery库中找到该函数,它看起来是这样的:

parseJSON: function( data ) {
    if ( typeof data !== "string" || !data ) {
        return null;
    }

    // Make sure leading/trailing whitespace is removed (IE can't handle it)
    data = jQuery.trim( data );

    // Attempt to parse using the native JSON parser first
    if ( window.JSON && window.JSON.parse ) {
        return window.JSON.parse( data );
    }

    // Make sure the incoming data is actual JSON
    // Logic borrowed from http://json.org/json2.js
    if ( rvalidchars.test( data.replace( rvalidescape, "@" )
        .replace( rvalidtokens, "]" )
        .replace( rvalidbraces, "")) ) {

        return ( new Function( "return " + data ) )();

    }
    jQuery.error( "Invalid JSON: " + data );
},
var json = '{ "firstname" : "john", "lastname" : "doe" }';
var person = jQuery.parseJSON(json);

console.log(person.firstname); //will show john
console.log(person.lastname); //will show doe
我如何通过它发送json

如果使用该函数,则可以访问API端点并在一次调用中解析响应

var obj = jQuery.parseJSON(yourJsonObj);
$.getJSON("/my_resource.json", function(data) {
  // Use data here
});
如果使用该函数,则可以访问API端点并在一次调用中解析响应

$.getJSON("/my_resource.json", function(data) {
  // Use data here
});
jQuery的parseJSON()函数将json转换为javascript对象

如果您的json是,例如:

{ "firstname" : "john", "lastname" : "doe" }
然后,当您使用parseJSON时,您可以访问如下属性:

parseJSON: function( data ) {
    if ( typeof data !== "string" || !data ) {
        return null;
    }

    // Make sure leading/trailing whitespace is removed (IE can't handle it)
    data = jQuery.trim( data );

    // Attempt to parse using the native JSON parser first
    if ( window.JSON && window.JSON.parse ) {
        return window.JSON.parse( data );
    }

    // Make sure the incoming data is actual JSON
    // Logic borrowed from http://json.org/json2.js
    if ( rvalidchars.test( data.replace( rvalidescape, "@" )
        .replace( rvalidtokens, "]" )
        .replace( rvalidbraces, "")) ) {

        return ( new Function( "return " + data ) )();

    }
    jQuery.error( "Invalid JSON: " + data );
},
var json = '{ "firstname" : "john", "lastname" : "doe" }';
var person = jQuery.parseJSON(json);

console.log(person.firstname); //will show john
console.log(person.lastname); //will show doe
这应该让你开始。有关更多信息,请阅读此处的文档:

jQuery的parseJSON()函数将json转换为javascript对象

如果您的json是,例如:

{ "firstname" : "john", "lastname" : "doe" }
然后,当您使用parseJSON时,您可以访问如下属性:

parseJSON: function( data ) {
    if ( typeof data !== "string" || !data ) {
        return null;
    }

    // Make sure leading/trailing whitespace is removed (IE can't handle it)
    data = jQuery.trim( data );

    // Attempt to parse using the native JSON parser first
    if ( window.JSON && window.JSON.parse ) {
        return window.JSON.parse( data );
    }

    // Make sure the incoming data is actual JSON
    // Logic borrowed from http://json.org/json2.js
    if ( rvalidchars.test( data.replace( rvalidescape, "@" )
        .replace( rvalidtokens, "]" )
        .replace( rvalidbraces, "")) ) {

        return ( new Function( "return " + data ) )();

    }
    jQuery.error( "Invalid JSON: " + data );
},
var json = '{ "firstname" : "john", "lastname" : "doe" }';
var person = jQuery.parseJSON(json);

console.log(person.firstname); //will show john
console.log(person.lastname); //will show doe

这应该让你开始。有关更多信息,请阅读此处的文档:

如果您使用jQuery AJAX命令,大多数命令都采用数据类型参数。将数据类型设置为“json”将自动解析返回的数据

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

在这种情况下,数据最终将成为一个基于AJAX调用返回的JSON的对象。

如果您使用的是jQuery AJAX命令,大多数命令都会使用数据类型参数。将数据类型设置为“json”将自动解析返回的数据

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});
在这种情况下,数据最终将成为一个基于AJAX调用返回的JSON的对象