Javascript 来自localstorage的JSON无法在php中解码

Javascript 来自localstorage的JSON无法在php中解码,javascript,php,json,html,local-storage,Javascript,Php,Json,Html,Local Storage,我正在从localstorage获取值作为JSON并尝试解码。 但它不起作用 这是我从localstorage获取JSON的代码: $items = "<script> document.write(JSON.parse(JSON.stringify(localStorage.getItem('itemList')))); </script>"; 对上面的json进行如下解码: {"items":[{"name":"Guitar","id":"1"}]} $decod

我正在从localstorage获取值作为JSON并尝试解码。 但它不起作用

这是我从localstorage获取JSON的代码:

$items = "<script> document.write(JSON.parse(JSON.stringify(localStorage.getItem('itemList')))); </script>";
对上面的json进行如下解码:

{"items":[{"name":"Guitar","id":"1"}]}
$decoded = json_decode($items, true);
当我尝试回显
$decoded
时,没有显示任何内容。当我做了
var\u dump($decoded)
its显示
NULL


感谢您的帮助。谢谢。

本地存储仅存储字符串。JSON.stringify()是错误的,因为它需要一个JSON对象,而不是字符串作为输入。

情况是,您无法“在一行”中组合JS/PHP

  • PHP部分(sample.PHP)


  • Em。。。你打算实现什么?这是一个完全错误的代码。字符串的行为与JS不同。。。这只是一根普通的绳子。必须将JS和PHP分开part@FieryCat-我正在尝试将localStoarge json值获取到php,并使用php@FieryCat-我能够从locaStorage获取JSON,尽管
    $decoded=JSON_decode(“{”items:[{”name:“Guitar”,“id:“1”}]}”,true)-可行;你的样本-never@FieryCat-我可以知道为什么吗?我应该只使用JSON.parse吗?你的命令看起来很奇怪。哪一行将JSON写入localstorage?是的,JSON.parse应该从localstorage字符串中为您提供一个JSON对象。只有
    JSON.parse
    提供了
    [object object]
    如果要打印JSON对象,您必须再次对其进行字符串化。好的,我们将尝试此代码不符合我的要求。但这是我检查过的正确代码
    $items = "<script> document.write(JSON.parse(JSON.stringify(localStorage.getItem('itemList')))); </script>";
    
    jQuery.post('sample.php', {
        'item': JSON.stringify(localStorage.getItem('itemList'))
    });
    
    $items = filter_input(INPUT_POST, 'item');
    $decoded = json_decode($items, true);
    var_dump($decoded);