I have a JS literal object string such as {name:{first:"George",middle:"William"},surname:"Washington"}
and I have to convert it in Json. How can I do it using PHP?
我有一个JS文字对象字符串,如{name:{first:“George”,middle:“William”},姓:“Washington”},我必须在Json中将其转换。我怎么能用PHP做到这一点?
2 个解决方案
#1
2
JS:
JS:
// Pretend we're POSTing this
var foo = {foo:{first:"George",middle:"William"}};
PHP:
PHP:
$foo = $_POST['foo'];
$foo = json_decode( stripslashes( $foo ) );
echo $foo->first;
Credit where credit is due: https://www.youtube.com/watch?v=pORFYsgOXog
信用到期的信用:https://www.youtube.com/watch?v = pORFYsgOXog
#2
-2
Not json_encode, use $var = json_decode($_POST['names'], true)
. You can then use it like echo $var['surname']
to echo "Washington".
不是json_encode,使用$ var = json_decode($ _ POST ['names'],true)。然后你可以像echo $ var ['surname']一样使用它来回应“华盛顿”。
#1
2
JS:
JS:
// Pretend we're POSTing this
var foo = {foo:{first:"George",middle:"William"}};
PHP:
PHP:
$foo = $_POST['foo'];
$foo = json_decode( stripslashes( $foo ) );
echo $foo->first;
Credit where credit is due: https://www.youtube.com/watch?v=pORFYsgOXog
信用到期的信用:https://www.youtube.com/watch?v = pORFYsgOXog
#2
-2
Not json_encode, use $var = json_decode($_POST['names'], true)
. You can then use it like echo $var['surname']
to echo "Washington".
不是json_encode,使用$ var = json_decode($ _ POST ['names'],true)。然后你可以像echo $ var ['surname']一样使用它来回应“华盛顿”。