如何从json,php解码值

时间:2018-03-08 12:31:25

标签: php json

我需要从移动应用程序解码下面的json

Array
(
    [{"unm":"admin","pw”:”password”}] => 
)

我的php代码是

$obj1 = print_r($_REQUEST, true); //get $_request variable data(responce of login) data as it is
foreach($obj1 as $key => $value)
{
    $obj2 = $key; //get first key
}
$obj3 = json_decode($obj2); //decode json data to obj3

$mob_user_name = $obj2['unm']; //getting json username field value
$mob_user_password = $obj2['pw']; //getting json password field value

2 个答案:

答案 0 :(得分:0)

希望这能解决你的问题, 注意:内容只不过是您从iOS应用中收到的内容

  

content = Array(       [{" unm":" admin"," pw“:”password“}] => )

php

中解析
$json = json_decode($content, true);
print json[0]['unm']; /* prints the username */
print json[0]['pw']; /* prints the password */

答案 1 :(得分:0)

{"unm":"admin","pw”:”password”}是一个对象,json_decode()默认情况下会构建它。

$obj = json_decode('{"unm":"admin","pw”:”password”}');
echo $obj->unm;
echo $obj->pw;

如果由于某种原因您希望将其转换为关联数组,请将json_decode()的第二个参数设置为the manual中指定的true

$arr = json_decode('{"unm":"admin","pw”:”password”}', true);
echo $arr['unm'];
echo $arr['pw'];