如何从json,php解码值

如何从json,php解码值

问题描述:

I need to decode the below json from a mobile app

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

and my php code is

$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

Hope this will fix your issue, Note: content is nothing but which you have received from iOS app

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

parse that in php

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

{"unm":"admin","pw”:”password”} is an object, and json_decode() will by default build it as so.

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

If for some reason you want it to be converted to an associative array, set the second parameter of json_decode() to true as specified in the manual.

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