Some day I wanted to check if the string is valid of JSON format. After some research I found the solution:

/**
 * Function isJSON
 * Check if a string is JSON
 */

if(!String.prototype.isJSON)
{
    String.prototype.isJSON = function()
    {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(this.replace(/"(\\.|[^"\\])*"/g,'')));
    }
}
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Comments

You can leave a response, or trackback from your own site.

Before you add comment see for rules.

Leave a Reply

Your email address will not be published. Required fields are marked *

2y6f0q