jQuery.parseJSON不工作,除非复制到一个不同的字符串变量

jQuery.parseJSON not working unless copied in a different string variable

本文关键字:一个 变量 字符串 工作 parseJSON jQuery 复制      更新时间:2023-09-26

我对javascript和jQuery相当陌生。我很确定我在这里错过了一些简单的东西。下面的代码将输出:

[{
    "cli_surname":"'u0392'u03bf'u03bd'u03b1'u03c0'u03ac'u03c1'u03c4'u03b7'u03c2",
    "cli_name":"'u039d'u03b1'u03c0'u03bf'u03bb'u03ad'u03c9'u03bd",
    "cli_sex":"M",
    "cli_dob":"1769-08-15",
    "cli_insurance":"1",
    "cli_phone":"9999999999",
    "cli_mobile":"9999999999",
    "cli_email":"bonaparte@hotmail.com",
    "cli_address":"'u0392'u03b1'u03c4'u03b5'u03c1'u03bb'u03ce 18",
    "ct_name":"'u0391'u03b3'u03af'u03b1 'u0392'u03b1'u03c1'u03b2'u03ac'u03c1'u03b1",
    "cli_comments":"'u039c'u03ad'u03b3'u03b1'u03c2"
}]

$("#userInfo"),但jQuery。parseJSON将失败。

$.ajax({        
    url: url,
    type: "POST",
    data: "c=" + selected.val(),
    success: function (json) {
        $("#userInfo").html(' ' + json);
        var obj = jQuery.parseJSON( json );
        alert( obj[0].cli_name );
    },
    error: function (xhr) {
        $("#userInfo").html('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
    }
}); 

现在,如果我从$("#userInfo")复制上述代码的输出,并将其粘贴到一个名为'data'的新变量中并解析它,它就可以工作了。

$.ajax({        
    url: url,
    type: "POST",
    data: "c=" + selected.val(),
    success: function (json) {
        $("#userInfo").html(' ' + json);
        data = '[{"cli_surname":"'u0392'u03bf'u03bd'u03b1'u03c0'u03ac'u03c1'u03c4'u03b7'u03c2","cli_name":"'u039d'u03b1'u03c0'u03bf'u03bb'u03ad'u03c9'u03bd","cli_sex":"M","cli_dob":"1769-08-15","cli_insurance":"1","cli_phone":"9999999999","cli_mobile":"9999999999","cli_email":"bonaparte@hotmail.com","cli_address":"'u0392'u03b1'u03c4'u03b5'u03c1'u03bb'u03ce 18","ct_name":"'u0391'u03b3'u03af'u03b1 'u0392'u03b1'u03c1'u03b2'u03ac'u03c1'u03b1","cli_comments":"'u039c'u03ad'u03b3'u03b1'u03c2"}]';
        var obj = jQuery.parseJSON( data );
        $("#userInfo").html(" " + data);
        alert( obj[0].cli_name );
    },
    error: function (xhr) {
        $("#userInfo").html('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
    }
});

对两个变量同时应用typeof,结果为string。到底发生了什么事?它是如何,我可以解析data变量,而不是json变量,因为它们都是字符串,似乎包含相同的数据?

Ajax调用将在内部为您进行解析。

所以,你的参数json到你的成功函数已经包含一个有效的javascript对象,这就是为什么解析失败:它是一个对象,而不是一个字符串。

构造$("#userInfo").html(' ' + json);然后将json再次转换为字符串,这就是为什么你在div中看到适当的内容。

改变:

$.ajax({        
    url: url,
    type: "POST",
    dataType: 'json', // this will force the response to be Json 
                      // even if MIME-Type tells something different!
    data: "c=" + selected.val(),
    success: function (json) {
        $("#userInfo").html(' ' + json);
        // unnecessary var obj = jQuery.parseJSON( json );
        alert( json[0].cli_name );
    },
    error: function (xhr) {
        $("#userInfo").html('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
    }
}); 

刚刚添加了dataType,以便在本地覆盖错误的MIME-Type响应头。根据第一个答案

试试这个:
响应将自动解析为json

          $.ajax({
                url: url,
                type: "POST",
                data: "c=" + selected.val(),
                dataType: 'json',     // this param talk that response will be parse automatically
                success: function(data) {
                    console.log(data)
                }
            });

你的json变量没有被解析,而你的数据变量正在被解析的原因是你的json变量有新的行字符('n)。

由于您的数据变量在一行中包含字符串,也就是说,没有新的行字符,该函数成功地解析了字符串。

我可能还应该指出,提供给JSON解析函数的字符串中的tab ('t)或任何其他转义字符将导致函数失败。

解析错误的字符串变量:

var myVar = "This is a text"; //will fail due to new line characters.
var myVar = 'My Friend''s birthday'; //will fail due to the "'" before "'"
var myVar = '   This is a text.' //will fail due to the tab character at the beginning of the line

" JSON标准不允许"控制字符",如制表符或换行符。"——http://api.jquery.com/jquery.parsejson/

查看上面的链接获取更多信息