views:

240

answers:

2

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|-?\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?

+1  A: 

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.

rosscj2533
I was also thinking about that (moving 3rd and 4th alert). But it fails anyway. The `parseJSON` function throws `"Invalid JSON: {"TermTitle":"some Title"}"` error. I also tried adding "your" code into the same function `ProcessFetchedTerm` and it works, but with the value from `opData` it does not work. Maybe it could be related to encoding issues...?
Priednis
I suppose it could somehow be an encoding issue, but I don't know what it would be. I would try debugging it when you are getting your `"Invalid JSON"` error, and trying to find a difference between the value of the `json` variable in my code and the value of `opData` in your code...
rosscj2533
A: 

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"?

Pointy