从Memcache返回的JSON无效


JSON returned from Memcache is invalid

我有Drupal 7网站。我使用Memcache缓存。

这是我如何存储JSON到它

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

没有问题,直到这里。但是当从缓存中获取JSON时。

返回的JSON无法验证

我使用http://jsonlint.com/来验证我的JSON。

请注意,JSON有正确的数据,但问题是验证。

$Records = $cache->get($cacheKey);
echo '<pre>';
print_r(Records);
exit();

任何帮助都非常感谢。

在var_dump()上返回的JSON如Jeroen在ans中提到的

string '{"defaults":[{"nid":"213","public_url":"http:'/'/www.mywebsite.com","current_ver'... (length=3033)

您在存储数据时使用serialize(),因此您需要在获取数据时使用unserialize():

$cache->set($cacheKey, serialize($jsonData));
...
$jsonData = unserialize($cache->get($cacheKey));

虽然没有必要序列化数据,因为Memcache会处理它:

$cache->set($cacheKey, $jsonData);
...
$jsonData = $cache->get($cacheKey);
编辑:

看看你到底有什么:

var_dump($cacheKey);
var_dump($jsonData);
$cache->set($cacheKey, $jsonData);
...
$jsonData = $cache->get($cacheKey);
var_dump($cacheKey);
var_dump($jsonData);