JSON returned from Memcache is invalid I have Drupal 7 site. I am using Memcache for caching.

This is how I am storing the JSON into it 

    //creating an object of Memcache
    $cache = new Memcache();
    $cache -&gt;addServer('localhost', 11211);
    //adding a key
    $cacheKey = 'mobile';
    //delete old cache
    $cache -&gt;delete($cacheKey);
    //refresh cache
    $cache -&gt;set($cacheKey, serialize($jsonData));


No issue till here. But when fetching the JSON from this cache. 

returned JSON fails to validate

I am using http://jsonlint.com/ to validate my JSON. 

Please note the however the JSON has correct data but issue is validation.

$Records = $cache-&gt;get($cacheKey);

echo '&lt;pre&gt;';
print_r(Records);
exit();


JSON

{
"defaults": [{
    "nid": "213",
    "public_url": "http:\/\/www.mywebsite.com",
    "HTMLStr": "layout and user interface.\
    r\ n\ t
    http: \ /\/mywebsite.com&lt;\/a&gt;&lt;\/p&gt;\r\n"
}]


}

Any help highly appreciated.
This is not really related to Drupal since you are not using the Drupal Memcache module API. 

But based on your example. Here is my test code.

  // Creating an object of Memcache
  $cache = new Memcache();
  $cache-&gt;addServer('localhost', 11211);
  // Adding a key.
  $cacheKey = 'mobile';
  // Delete old cache.
  $cache -&gt;delete($cacheKey);
  // Create a temp json data.
  $jsonData = "{
    'glossary': {
        'title': 'example glossary',
                'GlossDiv': {
            'title': 'S',
                        'GlossList': {
                'GlossEntry': {
                    'ID': 'SGML',
                                        'SortAs': 'SGML',
                                        'GlossTerm': 'Standard Generalized Markup Language',
                                        'Acronym': 'SGML',
                                        'Abbrev': 'ISO 8879:1986',
                                        'GlossDef': {
                        'para': 'A meta-markup language, used to create markup languages such as DocBook.',
                                                'GlossSeeAlso': ['GML', 'XML']
                      },
                                        'GlossSee': 'markup'
                  }
              }
          }
      }
  }";
  // Set the cache.
  $cache -&gt;set($cacheKey, serialize($jsonData));
  // Fetch the cache.
  $Records = $cache-&gt;get($cacheKey);

  // Dump the cache.
  echo '&lt;pre&gt;';
  print_r($Records);
  echo '&lt;/pre&gt;';


and here is the result

s:881:"{
    'glossary': {
        'title': 'example glossary',
                'GlossDiv': {
            'title': 'S',
                        'GlossList': {
                'GlossEntry': {
                    'ID': 'SGML',
                                        'SortAs': 'SGML',
                                        'GlossTerm': 'Standard Generalized Markup Language',
                                        'Acronym': 'SGML',
                                        'Abbrev': 'ISO 8879:1986',
                                        'GlossDef': {
                        'para': 'A meta-markup language, used to create markup languages such as DocBook.',
                                                'GlossSeeAlso': ['GML', 'XML']
                      },
                                        'GlossSee': 'markup'
                  }
              }
          }
      }
  }";


with $Records = unserialize($cache-&gt;get($cacheKey)); here is the result.

{
    'glossary': {
        'title': 'example glossary',
                'GlossDiv': {
            'title': 'S',
                        'GlossList': {
                'GlossEntry': {
                    'ID': 'SGML',
                                        'SortAs': 'SGML',
                                        'GlossTerm': 'Standard Generalized Markup Language',
                                        'Acronym': 'SGML',
                                        'Abbrev': 'ISO 8879:1986',
                                        'GlossDef': {
                        'para': 'A meta-markup language, used to create markup languages such as DocBook.',
                                                'GlossSeeAlso': ['GML', 'XML']
                      },
                                        'GlossSee': 'markup'
                  }
              }
          }
      }
  }


The only reason why you are not able to see the result is because of this.

print_r(Records);


which is it should be

print_r($Records);


Record is a variable and therefore it must have a dollar sign before it.

Test your connection to Memcache

  $cache = new Memcache();
  $cache-&gt;addServer('localhost', 11211);
  $stats = @$cache-&gt;getExtendedStats();
  $available = (bool) $stats["localhost:11211"];
  var_dump($available);


If the result is true then you are connected to Memcache if false then you are not connected.

UPDATE 1:

I tested with your json.

  $arr = array(
    "nid" =&gt; "213",
    "public_url" =&gt; "http:\/\/www.mywebsite.com",
    "HTMLStr" =&gt; "layout and user interface.\ r\ n\ t http: \ /\/mywebsite.com&lt;\/a&gt;&lt;\/p&gt;\r\n",
  );
  $jsonData = json_encode($arr, JSON_HEX_QUOT | JSON_HEX_APOS);


and here is the result

s:157:"{"nid":"213","public_url":"http:\\\/\\\/www.mywebsite.com","HTMLStr":"layout and user interface.\\ r\\ n\\ t http: \\ \/\\\/mywebsite.com&lt;\\\/a&gt;&lt;\\\/p&gt;\r\n"}";