1

My server returns a valid JSON string ( JSONP ).

eg parseJSON ( {"NAME":"Tom"} );

on client side i have this function implemented

function parseJSON ( myOBJ ) {

    //myOBJ is already a JSON object but I wanted to it to be a JSON string 
    // so that I can check
    // if browser supports JSON.parse otherwise do eval on that string.
}
1

1 Answer 1

3

myOBJ is already a JSON object but I wanted to it to be a JSON string so that I can check if browser supports JSON.parse otherwise do eval on that string.

If myObj is a JSON Object, the browser supports JSON (and JSON.parse), I would say. If you want to convert back to string, JSON.stringify. If you don't trust the browser, download json2.js. Don't use eval.

[edit]
In your case, seeing it's coming from a a JSONP request, the object such a request returns is a string, and that string is parsed into a script tag, after which it will be evaluated already. All browsers should support that, there's no client side JSON involved here I think.

Sign up to request clarification or add additional context in comments.

2 Comments

THanks for your reply. THe problem is I don't know the behavior when JSON.parse is not supported by the browser. How do I write code within parseJSON function to handle this situation. Will myObj be a string still.
Hi kaushal kishore, if the browser doesn't support JSON natively, you'll need an external script to parse it (like json2.js). But see my edits to the answer also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.