开发者

The regex test in jQuery's parseJSON returns false - why?

I don't understand why the test in jQuery's parseJSON function:

/^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
            .replace(/"[^"\\\n\r]*"|true|false|null|-开发者_StackOverflow中文版?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
            .replace(/(?:^|:|,)(?:\s*\[)+/g, ""))

returns false for the string: {"TermTitle":"some Title"}

The test in http://www.jsonlint.com/ tells me that the string {"TermTitle":"some Title"} is valid JSON, but when I try to pass it into $.parseJSON(opData) the parseJSON function fails...

I also tested just this /^[\],:{}\s]*$/.test... function separately in Firebug with the mentioned string.

[edit] Ok, code:

var json = '{"TermTitle":"some Title"}';  
var obj = $.parseJSON(json);  
alert('obj = ' + obj + ' and obj.TermTitle = ' + obj.TermTitle);

works also for me.

But in my case where I have the following in my JavaScript:

function GetTerm($iInd) {  
    $.ajax({  
        type: "POST",  
        url: "get_term.php",  
        data: {  
            id: $iInd  
        },  
        success: function(data){  
            ProcessFetchedTerm(data);  
        }  
    });  
    //If I tried adding dataType: "json" then it would stop working at all.
}  

function ProcessFetchedTerm(opData) {  
    alert ("In ProcessFetchedTerm, data: '" + opData + "'");  
    alert ("typeof opData: " + typeof opData);  

    alert ("Replacement result of the regex function: " +  
      opData.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@").  
          replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]").  
          replace(/(?:^|:|,)(?:\s*\[)+/g, ''));  

    alert ("Result of the regex function: " +  
      /^[\],:{}\s]*$/.  
          test(opData.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@").  
          replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]").  
          replace(/(?:^|:|,)(?:\s*\[)+/g, '')));  

    var oTerm = $.parseJSON(opData);  
    alert('oTerm = ' + oTerm + ' and oTerm.TermTitle = ' + oTerm.TermTitle);  
}  

and in get_term.php I have:

echo json_encode(  
    array(  
        "TermTitle"=>"some Title"  
    )  
);  

The alerts return:

In ProcessFetchedTerm, data: '{"TermTitle":"some Title"}'

typeof opData: string

Replacement result of the regex function: {]:]}

Result of the regex function: false

The last alert is not shown

[edit 2]

I rewrote the beginning of my function ProcessFetchedTerm to

function ProcessFetchedTerm(opData) {  

    var json = '{"TermTitle":"some Title"}';  
    alert ("In ProcessFetchedTerm, opData: '" + opData + "' json: '" + json + "'");  

    var oTerm = $.parseJSON(json);  

the alert puts out:

In ProcessFetchedTerm, opData: '{"TermTitle":"some Title"}' json: '{"TermTitle":"some Title"}'  

So the strings are equal and in case the next line is var oTerm = $.parseJSON(json); it works but if next line is var oTerm = $.parseJSON(opData); it does not work.

So what could be hidden inside this (presumably) string object opData that prevents it from working?


Running this code:

var json = '{"TermTitle":"some Title"}';
var obj = $.parseJSON(json);
alert('obj = ' + obj + ' and obj.TermTitle = ' + obj.TermTitle);

works for me (with jQuery 1.4). The problem must be elsewhere in your code. What is the exact code you are running when the parseJSON method fails?

Edit (response to posted code): Why are you doing the functionality of your 3rd and 4th alert? I would guess that that is changing your opData value. Try adding your first alert right before your parseJSON call and see what it is there.


It returns "false" because it doesn't match. The "replace" calls transform that JSON string into `{]:]}', which does not match the regex.

[edit] oh durrr yes it does match; well I don't know what your problem is. Why do you think it returns "false"?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜